怎样把GridView中的数据导出成Excel表格? 30
我从数据库表中查出数据,反内容显示在GridView中,我再在想实现一个功能,就是把GridView中的数据导出成Excel表格,请问怎么实现,谢谢...
我从数据库表中查出数据,反内容显示在GridView中,我再在想实现一个功能,就是把GridView中的数据导出成Excel表格,请问怎么实现,谢谢
展开
展开全部
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
object ms = Type.Missing;
Excel.Workbook wk = excel.Workbooks.Add(ms);
Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
ws.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
}
}
if (System.IO.File.Exists(this.MapPath("\\userField\\empinfo.xls")))
{
System.IO.File.Delete(this.MapPath("\\userField\\empinfo.xls"));
}
wk.SaveAs(this.MapPath("\\userField\\empinfo.xls"), ms, ms, ms, ms, ms, Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms);
excel.Quit();
this.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "window.open('../userField/empinfo.xls');", true);
Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
object ms = Type.Missing;
Excel.Workbook wk = excel.Workbooks.Add(ms);
Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
ws.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
}
}
if (System.IO.File.Exists(this.MapPath("\\userField\\empinfo.xls")))
{
System.IO.File.Delete(this.MapPath("\\userField\\empinfo.xls"));
}
wk.SaveAs(this.MapPath("\\userField\\empinfo.xls"), ms, ms, ms, ms, ms, Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms);
excel.Quit();
this.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "window.open('../userField/empinfo.xls');", true);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询