vs2005 Dataset导出EXCEL vs2005 Dataset导出EXCEL有没这方面的代码,搜索了几个,都不能用 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 希望是任何版本的EXCEL都能用的 asp.net直接输出为excel文件就可以了,如果是windowsform 需要调用excel的com对象 1.加入对excel的参考2.using Microsoft.Office.Interop.Excel; ApplicationClass excel = new ApplicationClass(); excel.Application.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); for (int i = 1; i <= ds.Tables[0].Columns.Count; i++) { excel.Cells[1, i] = ds.Tables[0].Columns[i - 1].ColumnName.ToString().ToUpper(); } for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { excel.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString(); } } excel.Visible = true; excel.ScreenUpdating = true; private static void ExportToExcel(DataSet ds) { object oMissing = System.Reflection.Missing.Value; Excel.ApplicationClass xlApp = new Excel.ApplicationClass(); try { // 打开Excel文件。 Excel.Workbook xlWorkbook =xlApp.Workbooks.Add(1); Excel.Worksheet xlWorksheet; // 循环所有DataTable for( int i=0; i<ds.Tables.Count; i++ ) { // 添加入一个新的Sheet页 xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing); xlWorksheet.Name = ds.Tables[i].TableName; for (int j = 0; j < ds.Tables[i].Columns.Count; j++) //写列标题 { xlWorksheet.Cells[1, j + 1] = ds.Tables[i].Columns[j].ColumnName; } for (int r = 0; r < ds.Tables[i].Rows.Count; r++) //写值 { for (int m = 0; m < ds.Tables[i].Columns.Count; m++) { xlWorksheet.Cells[r + 2, m + 1] = ds.Tables[i].Rows[r][m]; } } } xlApp.Visible=true; } catch { } } *****************************************************************************欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html 如何用C#代码创建XML文件 winfrom里写正则表达式的问题 请问怎么制作数据库软件的安装向导? 一个关于access的mda,mdw文件的问题 急…… panel如何滚动? [请问]为什么MouseWheel事件有时无法响应? 高难度SQL统计问题 哪个哥们牛逼,进来看看! 为什么用"+="而不用"="? listview,急,急,急,急! 窗体间的值传递
2.
using Microsoft.Office.Interop.Excel; ApplicationClass excel = new ApplicationClass();
excel.Application.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
for (int i = 1; i <= ds.Tables[0].Columns.Count; i++)
{
excel.Cells[1, i] = ds.Tables[0].Columns[i - 1].ColumnName.ToString().ToUpper();
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
excel.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
}
}
excel.Visible = true;
excel.ScreenUpdating = true;
private static void ExportToExcel(DataSet ds)
{
object oMissing = System.Reflection.Missing.Value;
Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
try
{
// 打开Excel文件。
Excel.Workbook xlWorkbook =xlApp.Workbooks.Add(1);
Excel.Worksheet xlWorksheet;
// 循环所有DataTable
for( int i=0; i<ds.Tables.Count; i++ )
{
// 添加入一个新的Sheet页
xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing);
xlWorksheet.Name = ds.Tables[i].TableName;
for (int j = 0; j < ds.Tables[i].Columns.Count; j++) //写列标题
{
xlWorksheet.Cells[1, j + 1] = ds.Tables[i].Columns[j].ColumnName;
}
for (int r = 0; r < ds.Tables[i].Rows.Count; r++) //写值
{
for (int m = 0; m < ds.Tables[i].Columns.Count; m++)
{
xlWorksheet.Cells[r + 2, m + 1] = ds.Tables[i].Rows[r][m];
}
}
}
xlApp.Visible=true;
}
catch
{
} }
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html