------一简单的导出数据到Excel的程序-------! 如题,求在WinForm中将DataSet中的数据导入到Excel中的程序。 强调一下,是在WinForm中,另外希望速度不会太慢。 急,请做过的兄弟把代码贴给我,谢谢,分不多了,在线等,马上结帖。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void ExportToExcel(string filePath, System.Data.DataTable ds) { object oMissing = System.Reflection.Missing.Value; Excel.ApplicationClass xlApp = new Excel.ApplicationClass(); try { // 打开Excel文件。以下为Office 2000. Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,oMissing,oMissing,oMissing); Excel.Worksheet xlWorksheet; // 循环所有DataTable // 添加入一个新的Sheet页。 xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing); // 以TableName作为新加的Sheet页名。 xlWorksheet.Name = ds.TableName; // 取出这个DataTable中的所有值,暂存于stringBuffer中。 string stringBuffer = ""; for( int j=0; j<ds.Rows.Count; j++ ) { for( int k=0; k<ds.Columns.Count; k++ ) { stringBuffer += ds.Rows[j][k].ToString(); if( k < ds.Columns.Count - 1 ) stringBuffer += "\t"; } stringBuffer += "\n"; } // 利用系统剪切板 System.Windows.Forms.Clipboard.SetDataObject(""); // 将stringBuffer放入剪切板。 System.Windows.Forms.Clipboard.SetDataObject(stringBuffer); // 选中这个sheet页中的第一个单元格 ((Excel.Range)xlWorksheet.Cells[1,1]).Select(); // 粘贴! xlWorksheet.Paste(oMissing,oMissing); // 清空系统剪切板。 System.Windows.Forms.Clipboard.SetDataObject(""); // 保存并关闭这个工作簿。 xlWorkbook.Close( Excel.XlSaveAction.xlSaveChanges, oMissing, oMissing ); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook); xlWorkbook = null; } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { // 释放... xlApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlApp = null; GC.Collect(); } } /// <summary> /// 将DataSet里所有数据导入Excel. /// 需要添加COM: Microsoft Excel Object Library. /// using Excel; /// </summary> /// <param name="filePath"></param> /// <param name="ds"></param> public static void ExportToExcel(string filePath, DataSet ds) { object oMissing = System.Reflection.Missing.Value; Excel.ApplicationClass xlApp = new Excel.ApplicationClass(); try { // 打开Excel文件。以下为Office 2000. Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,oMissing,oMissing,oMissing); Excel.Worksheet xlWorksheet; // 循环所有DataTable for( int i=0; i<ds.Tables.Count; i++ ) { // 添加入一个新的Sheet页。 xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing); // 以TableName作为新加的Sheet页名。 xlWorksheet.Name = ds.Tables[i].TableName; // 取出这个DataTable中的所有值,暂存于stringBuffer中。 string stringBuffer = ""; for( int j=0; j<ds.Tables[i].Rows.Count; j++ ) { for( int k=0; k<ds.Tables[i].Columns.Count; k++ ) { stringBuffer += ds.Tables[i].Rows[j][k].ToString(); if( k < ds.Tables[i].Columns.Count - 1 ) stringBuffer += "\t"; } stringBuffer += "\n"; } // 利用系统剪切板 System.Windows.Forms.Clipboard.SetDataObject(""); // 将stringBuffer放入剪切板。 System.Windows.Forms.Clipboard.SetDataObject(stringBuffer); // 选中这个sheet页中的第一个单元格 ((Excel.Range)xlWorksheet.Cells[1,1]).Select(); // 粘贴! xlWorksheet.Paste(oMissing,oMissing); // 清空系统剪切板。 System.Windows.Forms.Clipboard.SetDataObject(""); } // 保存并关闭这个工作簿。 xlWorkbook.Close( Excel.XlSaveAction.xlSaveChanges, oMissing, oMissing ); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook); xlWorkbook = null; } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { // 释放... xlApp.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlApp = null; GC.Collect(); } } 请问上面给出的是WINFORM中用的吗我要的是winform下能用的。 上面的是winform 中用的吗? up另外问下,上面的那调用时候ExportToExcel(string filePath, DataSet ds);filePath这个参数填什么呢?是什么的路径呢?谢谢。之前从来没做过winform. TreeView遍历问题 求教C#自学入门方法 long类型 问题 各位前辈请教一个小问题 关于C#与其他设备通讯的问题 Entity framework String 和 DateTime的对比 webclient首次连接下载文件特别慢 如何用C#语句执行一个sql脚本文件? 请问怎么和串口进行通信?只用C#能实现吗?对这个一直有兴趣, 这个.cs???它的显示是空白的! 使用<httpModules>节点时产生的验证码不能显示,为什么? 如何在B窗体中使用A窗体的变量?
{
object oMissing = System.Reflection.Missing.Value;
Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
try
{
// 打开Excel文件。以下为Office 2000.
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,oMissing,oMissing,oMissing);
Excel.Worksheet xlWorksheet;
// 循环所有DataTable
// 添加入一个新的Sheet页。
xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing);
// 以TableName作为新加的Sheet页名。
xlWorksheet.Name = ds.TableName;
// 取出这个DataTable中的所有值,暂存于stringBuffer中。
string stringBuffer = "";
for( int j=0; j<ds.Rows.Count; j++ )
{
for( int k=0; k<ds.Columns.Count; k++ )
{
stringBuffer += ds.Rows[j][k].ToString();
if( k < ds.Columns.Count - 1 )
stringBuffer += "\t";
}
stringBuffer += "\n";
}
// 利用系统剪切板
System.Windows.Forms.Clipboard.SetDataObject("");
// 将stringBuffer放入剪切板。
System.Windows.Forms.Clipboard.SetDataObject(stringBuffer);
// 选中这个sheet页中的第一个单元格
((Excel.Range)xlWorksheet.Cells[1,1]).Select();
// 粘贴!
xlWorksheet.Paste(oMissing,oMissing);
// 清空系统剪切板。
System.Windows.Forms.Clipboard.SetDataObject("");
// 保存并关闭这个工作簿。
xlWorkbook.Close( Excel.XlSaveAction.xlSaveChanges, oMissing, oMissing );
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);
xlWorkbook = null;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// 释放...
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
GC.Collect();
}
}
/// 将DataSet里所有数据导入Excel.
/// 需要添加COM: Microsoft Excel Object Library.
/// using Excel;
/// </summary>
/// <param name="filePath"></param>
/// <param name="ds"></param>
public static void ExportToExcel(string filePath, DataSet ds)
{
object oMissing = System.Reflection.Missing.Value;
Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
try
{
// 打开Excel文件。以下为Office 2000.
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,oMissing,oMissing,oMissing);
Excel.Worksheet xlWorksheet;
// 循环所有DataTable
for( int i=0; i<ds.Tables.Count; i++ )
{
// 添加入一个新的Sheet页。
xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing);
// 以TableName作为新加的Sheet页名。
xlWorksheet.Name = ds.Tables[i].TableName;
// 取出这个DataTable中的所有值,暂存于stringBuffer中。
string stringBuffer = "";
for( int j=0; j<ds.Tables[i].Rows.Count; j++ )
{
for( int k=0; k<ds.Tables[i].Columns.Count; k++ )
{
stringBuffer += ds.Tables[i].Rows[j][k].ToString();
if( k < ds.Tables[i].Columns.Count - 1 )
stringBuffer += "\t";
}
stringBuffer += "\n";
}
// 利用系统剪切板
System.Windows.Forms.Clipboard.SetDataObject("");
// 将stringBuffer放入剪切板。
System.Windows.Forms.Clipboard.SetDataObject(stringBuffer);
// 选中这个sheet页中的第一个单元格
((Excel.Range)xlWorksheet.Cells[1,1]).Select();
// 粘贴!
xlWorksheet.Paste(oMissing,oMissing);
// 清空系统剪切板。
System.Windows.Forms.Clipboard.SetDataObject("");
}
// 保存并关闭这个工作簿。
xlWorkbook.Close( Excel.XlSaveAction.xlSaveChanges, oMissing, oMissing );
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);
xlWorkbook = null;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// 释放...
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
GC.Collect();
}
}
请问上面给出的是WINFORM中用的吗
我要的是winform下能用的。
另外问下,上面的那调用时候ExportToExcel(string filePath, DataSet ds);
filePath这个参数填什么呢?是什么的路径呢?谢谢。之前从来没做过winform.