第一读取Datatable的表结构,写入sheet里,然后是填充数据dataset中的数据保存到excel中" src="/uploads/tu" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 有一个<em>DataSet</em>,里面包含多个DataTable,需要导...-datas

有一个<em>DataSet</em>,里面包含多个DataTable,需要导...-datas

作者:乔山办公网日期:

返回目录:excel表格制作





第一读取Datatable的表结构,写入sheet里,然后是填充数据

将DataSet中的DataTable作为参数传入下面的e799bee5baa6e997aee7ad94e4b893e5b19e335方法,再将你要保存的完整路径名作为第二个参数传入下面的方法,就可以将数据导入到Excel中
public void CreateExcel(DataTable dt,string fileName)
{
System.Diagnostics.Process[] arrProcesses;
arrProcesses = System.Diagnostics.Process.GetProcessesByName("Excel");
foreach (System.Diagnostics.Process myProcess in arrProcesses)
{
myProcess.Kill();
}

Object missing = Missing.Value;
Microsoft.Office.Interop.Excel.Application m_objExcel =

new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks m_objWorkBooks = m_objExcel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook m_objWorkBook = m_objWorkBooks.Add(true);
Microsoft.Office.Interop.Excel.Sheets m_objWorkSheets = m_objWorkBook.Sheets; ;
Microsoft.Office.Interop.Excel.Worksheet m_objWorkSheet =

(Microsoft.Office.Interop.Excel.Worksheet)m_objWorkSheets[1];
int intFeildCount = dt.Columns.Count;
for (int col = 0; col < intFeildCount; col++)
{
m_objWorkSheet.Cells[1, col + 1] = dt.Columns[col].ToString();
}
for (int intRowCount = 0; intRowCount < dt.Rows.Count; intRowCount++)
{
for (int intCol = 0; intCol < dt.Columns.Count; intCol++)
{
m_objWorkSheet.Cells[intRowCount + 2, intCol + 1] = "'"+dt.Rows[intRowCount][intCol].ToString();
}
}

if (File.Exists(fileName))
{
File.Delete(fileName);
}
m_objWorkBook.SaveAs(fileName, missing, missing, missing, missing,

missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);

m_objExcel = null;

}

相关阅读

关键词不能为空
极力推荐
  • 怎样在<em>excel</em>中加入<em>超链接</em>啊,我弄

  • excel超链接打不开,超链接后点击出现无法打开指定的文件,..." src="/uploads/tu/171.jpg" style="width: 400px; height: 267px;" />1、双击打开需要设置超链接的表格,进入表格。点击选中需要设置超链

ppt怎么做_excel表格制作_office365_word文档_365办公网