猿问

在页面中调用导出Excel 表格

现在 页面中有许多的数据  需要设置个按钮 然后当点击按钮后提示 是否导出Excel表格 点击是 将页面中的数据导出 下载下来 是Excel表格 格式的 又会的么  告诉下 谢谢

慕森卡
浏览 587回答 2
2回答

繁星coding

public class DataTableToExcel &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp;/// <summary> &nbsp; &nbsp; &nbsp; &nbsp;/// DataSet中数据导出到Excel &nbsp; &nbsp; &nbsp; &nbsp;/// </summary> &nbsp; &nbsp; &nbsp; &nbsp;/// <param name="gridView">DataSet对象</param> &nbsp; &nbsp; &nbsp; &nbsp;/// <param name="isShowExcle">是否显示Excel界面</param> &nbsp; &nbsp; &nbsp; &nbsp;/// <returns></returns> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//导出Excel &nbsp; &nbsp; &nbsp; &nbsp;public static void ExportExcel(System.Data.DataTable dt) &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Excel.Application excelKccx = new Excel.Application(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;excelKccx.Workbooks.Add(true); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int i = 0; i < dt.Columns.Count; i++)//取字段名 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;excelKccx.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int i = 0; i < dt.Rows.Count; i++)//取记录值 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int j = 0; j < dt.Columns.Count; j++) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;excelKccx.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;excelKccx.Visible = true; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp;}调用方法:DataSet ds=null;ds = db.getDataSet(sql);DataTable dt = ds.Tables[0];DataTableToExcel.ExportExcel(dt);

一只斗牛犬

一种方法是把当前页面或HTML元素输出为excel文件,这种方法不标准。另一种用第三方组件来操作,这种方法稍麻烦。
随时随地看视频慕课网APP
我要回答