将Textbox、listview控件里的数据导入Excel
注:先要添加microsoft.Office.Interop.Excel 11.0版本引用,若没有此引用,请到此下载:http://api.dllzj.com/search/search.aspx?name=microsoft.office.interop.excel.dll
将Textbox控件里的数据导入Excel(windowsForm中):
/// <summary>
/// 把数据导入到Excel中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "xls"; //设置默认扩展名为xls
sfd.Filter = "Excel文件(*.xls)|*.xls";//另存文件时文件类型框中出现的内容
if (sfd.ShowDialog() == DialogResult.OK) //获取选定的另存文件对话框存在
{
DoExport(this.textBox2, sfd.FileName);
}
}
private void DoExport(TextBox textbox, string strFileName)
{
// string[] lines = System.Text.RegularExpressions.Regex.Split(textbox.Text, "/r/n");
// int rowNum = lines.Length;//获取 TextBox2控件里的数据行数
int rowNum=textbox.Lines.Length; //读取Textbox控件里数据的行数
int rowIndex = 1; //定义一个Excel行
int col = 1; //定义一个Excel列,默认为1
if (rowNum == 0 || string.IsNullOrEmpty(strFileName)) //控件里没有数据
{
return;
}
if (rowNum > 0)//控件里有数据
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的系统未安装Excel");
return;
}
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
for (int i = 0; i < rowNum; i++)//将数据导入Excel
{
string ss = textbox.Lines[i].ToString(); //读取相对应行的数据,这里只是测试用
rowIndex++;
xlApp.Cells[rowIndex,col] = Convert.ToString(textbox.Lines[i].ToString()) + "\t";//读取到的Textbox控件里的数据导入到Excel
//xlApp.Rows[rowIndex,0] = Convert.ToString(textbox.Lines[i].ToString()) + "\t";
}
xlBook.SaveAs(strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlApp = null;
xlBook = null;
MessageBox.Show("数据导入成功!!");
}
}
将listview控件里的数据导入Excel中:
/// <summary>
/// 执行导出数据
/// </summary>
public void ExportToExecl()
{
System.Windows.Forms.SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "xls";
sfd.Filter = "Excel文件(*.xls)|*.xls";
if (sfd.ShowDialog() == DialogResult.OK)
{
DoExport(this.lstPostion, sfd.FileName);
}
}
/// <summary>
/// 具体导出的方法
/// </summary>
/// <param name="listView">ListView</param>
/// <param name="strFileName">导出到的文件名</param>
private void DoExport(ListView listView, string strFileName)
{
int rowNum = listView.Items.Count;
int columnNum = listView.Items[0].SubItems.Count;
int rowIndex = 1;
int columnIndex = 0;
if (rowNum == 0 || string.IsNullOrEmpty(strFileName))
{
return;
}
if (rowNum > 0)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
if (xlApp == null)
{
MessageBox.Show("无法创建excel对象,可能您的系统没有安装excel");
return;
}
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
//将ListView的列名导入Excel表第一行
foreach (ColumnHeader dc in listView.Columns)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = dc.Text;
}
//将ListView中的数据导入Excel中
// xlApp.Cells[rowIndex, columnIndex] = Convert.ToString(this.textBox1.Text.Trim());
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
//注意这个在导出的时候加了“\t” 的目的就是避免导出的数据显示为科学计数法。可以放在每行的首尾。
xlApp.Cells[rowIndex, columnIndex] = Convert.ToString(999999) + "\t";
}
}
//例外需要说明的是用strFileName,Excel.XlFileFormat.xlExcel9795保存方式时 当你的Excel版本不是95、97 而是2003、2007 时导出的时候会报一个错误:异常来自 HRESULT:0x800A03EC。 解决办法就是换成strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal。
xlBook.SaveAs(strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlApp = null;
xlBook = null;
MessageBox.Show("OK");
}
}