关于导出到excel的问题 您好,我的老师让我实现这样一个功能,把数据库中的表导出到excel,比如一个学生成绩信息表吧,把每个学生的各科成绩都导出来,在excel表的后面还要对各科进行统计,对及格率,优秀等等也进行统计,您能给我一个这样的例子么,或者给我一个具体的实现思路 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 导出的只是数据源,统计和数据都是数据库事先统计好再导出的http://www.wangchao.net.cn/bbsdetail_545898.html 有现成的控件嘛?直接将获取的数据集导到excel里。。不过如果你是学生的话,最好还是手写比较好。。 把数据绑定一个GRIDVIEW里,并GRIDVIEW隐藏起来,再导出,应该能实现。方法比较笨,但能实现 #region 用OFFICE組件導出EXCEL存放到批定目錄 /// <summary> /// 異出Excel /// </summary> /// <param name="tmpDataTable">數據表</param> /// <param name="strFileName">異出路徑</param> public static void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName) { if (tmpDataTable == null) return; int rowNum = tmpDataTable.Rows.Count; int columnNum = tmpDataTable.Columns.Count; int rowIndex = 1; int columnIndex = 0; Application xlApp = new ApplicationClass(); xlApp.DefaultFilePath = ""; xlApp.DisplayAlerts = true; xlApp.SheetsInNewWorkbook = 1; Workbook xlBook = xlApp.Workbooks.Add(true); //将DataTable的列名导入Excel表第一行 foreach (DataColumn dc in tmpDataTable.Columns) { columnIndex++; xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName; } //将DataTable中的数据导入Excel中 for (int i = 0; i < rowNum; i++) { rowIndex++; columnIndex = 0; for (int j = 0; j < columnNum; j++) { columnIndex++; xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString(); } } //xlBook.SaveCopyAs(HttpUtility.UrlDecode(strFileName, System.Text.Encoding.UTF8)); xlBook.SaveCopyAs(strFileName); xlBook.Close(false, Type.Missing, Type.Missing); xlApp.Workbooks.Close(); xlApp.Quit(); if (xlBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook); xlApp = null; } if (xlApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); xlApp = null; } //System.Diagnostics.Process[] P1 = System.Diagnostics.Process.GetProcessesByName("Excel"); //for (int i = 0; i < P1.Length; i++) //{ // P1[i].Kill(); //} GC.Collect(); } #endregion 关于IE8下兼容的问题。 出错。求修改 thebeerhouse代码悬赏求解,象征性20元人民币报酬,支付宝支付 treeview gridvew批量删除问题 关于updatepanel的问题 DataSourceID的问题 数据表 一个非技术问题,对域名了解的朋友进来看看,来者有分 在线等待,请高手们帮忙 推荐阅读系统一般都是怎么做的? 项目在office2010下开发 在office2003下出bug
方法比较笨,但能实现
/// <summary>
/// 異出Excel
/// </summary>
/// <param name="tmpDataTable">數據表</param>
/// <param name="strFileName">異出路徑</param>
public static void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
return;
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0;
Application xlApp = new ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Workbook xlBook = xlApp.Workbooks.Add(true);
//将DataTable的列名导入Excel表第一行
foreach (DataColumn dc in tmpDataTable.Columns)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
}
//将DataTable中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();
}
}
//xlBook.SaveCopyAs(HttpUtility.UrlDecode(strFileName, System.Text.Encoding.UTF8));
xlBook.SaveCopyAs(strFileName);
xlBook.Close(false, Type.Missing, Type.Missing);
xlApp.Workbooks.Close();
xlApp.Quit();
if (xlBook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook);
xlApp = null;
}
if (xlApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
//System.Diagnostics.Process[] P1 = System.Diagnostics.Process.GetProcessesByName("Excel");
//for (int i = 0; i < P1.Length; i++)
//{
// P1[i].Kill();
//}
GC.Collect();
}
#endregion