怎样把GridView中的数据导出成Excel表格?
来源:百度知道 编辑:UC知道 时间:2024/06/16 09:36:39
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")))