如何把一个dataset中的多张table导入到excel里的不同sheets

owl1986 2011-10-11 03:07:20
请问如何把一个dataset中的多张table导入到excel里的不同sheets呢.每张表对应一个相应的sheet
...全文
74 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysudongxi 2011-10-11
  • 打赏
  • 举报
回复

Microsoft.Office.Interop.Excel.Application app =
new Microsoft.Office.Interop.Excel.ApplicationClass();
Object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Workbooks m_objWorkBooks = app.Workbooks;
Microsoft.Office.Interop.Excel.Workbook m_objWorkBook = m_objWorkBooks.Add(true);
//Microsoft.Office.Interop.Excel.Sheets m_objWorkSheets = m_objWorkBook.Sheets;
m_objWorkBook = app.Workbooks.Open(sourcefilePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Microsoft.Office.Interop.Excel.Worksheet m_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objWorkBook.Sheets[1];
try
{
app.Visible = false;
if (excelTable.Rows.Count > 0)
{
int row = 0;
row = excelTable.Rows.Count;
int col = excelTable.Columns.Count;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
string str = excelTable.Rows[i][j].ToString();
m_objWorkSheet.Cells[i + 3, j + 1] = str;
}
}
}
// 设置禁止弹出保存和覆盖的询问提示框
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
//保存excel文件
m_objWorkSheet.SaveAs(sourcefilePath, missing, missing, missing, missing, missing, missing, missing, missing, missing);
//app.PrintPreview();
app.Quit();
app = null;

不同sheet设定活动状态Microsoft.Office.Interop.Excel.Worksheet m_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objWorkBook.Sheets[1];
sheet[1],sheet[2]..
owl1986 2011-10-11
  • 打赏
  • 举报
回复
excel中sheet的名字是固定的,要把对应的table导进去...
ysudongxi 2011-10-11
  • 打赏
  • 举报
回复
dataset.Tables[i].Rows[][]不同table单元格的内容依次写到excel的不同sheet中。
你可以搜搜如何往excel中导入数据。再按这个试试。

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧