2 回答
TA贡献1797条经验 获得超4个赞
public class DataTableToExcel
{
/// <summary>
/// DataSet中数据导出到Excel
/// </summary>
/// <param name="gridView">DataSet对象</param>
/// <param name="isShowExcle">是否显示Excel界面</param>
/// <returns></returns>
//导出Excel
public static void ExportExcel(System.Data.DataTable dt)
{
Excel.Application excelKccx = new Excel.Application();
excelKccx.Workbooks.Add(true);
for (int i = 0; i < dt.Columns.Count; i++)//取字段名
{
excelKccx.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString();
}
for (int i = 0; i < dt.Rows.Count; i++)//取记录值
{
for (int j = 0; j < dt.Columns.Count; j++)
{
excelKccx.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
}
}
excelKccx.Visible = true;
}
}
调用方法:
DataSet ds=null;
ds = db.getDataSet(sql);
DataTable dt = ds.Tables[0];
DataTableToExcel.ExportExcel(dt);
- 2 回答
- 0 关注
- 586 浏览
添加回答
举报