c#将DataTable中的数据导出到EXCEL中,怎样才能快速导出,求实例
答案:2 悬赏:20 手机版
解决时间 2021-01-25 16:37
- 提问者网友:动次大次蹦擦擦
- 2021-01-25 04:05
c#将DataTable中的数据导出到EXCEL中,怎样才能快速导出,求实例
最佳答案
- 五星知识达人网友:罪歌
- 2021-01-25 04:19
你搜索并参考 datagridview导出到excel
原理是一样 的
原理是一样 的
全部回答
- 1楼网友:街头电车
- 2021-01-25 04:56
///
/// 导出excel
///
/// 导出的数据源
/// excel的名称
/// excel的标题
///
public static void datatabletoexcel(datatable dt, string title, string fname, httpresponse resp)
{
excelengine excelengine = new excelengine();
iapplication application = excelengine.excel;
iworkbook workbook = application.workbooks.create(1);
iworksheet sheet = workbook.worksheets[0];
sheet.importdatatable(dt, true, 3, 1, -1, -1);
//header style
istyle headerstyle = workbook.styles.add("headerstyle");
headerstyle.beginupdate();
//add custom colors to the palette.
//workbook.setpalettecolor(8, color.fromargb(182, 189, 218));
headerstyle.color = color.darkblue;
headerstyle.font.bold = true;
headerstyle.font.color = excelknowncolors.white;
headerstyle.borders[excelbordersindex.edgeleft].linestyle = excellinestyle.thin;
headerstyle.borders[excelbordersindex.edgeright].linestyle = excellinestyle.thin;
headerstyle.borders[excelbordersindex.edgetop].linestyle = excellinestyle.thin;
headerstyle.borders[excelbordersindex.edgebottom].linestyle = excellinestyle.thin;
//apply style
char[] constant = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a','b','c','d','e','f','g','h','i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r','s', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
sheet.range["a1:" + constant[dt.columns.count - 1] + "1"].cellstylename = "headerstyle";
sheet.range["a3:" + constant[dt.columns.count - 1] + "3"].cellstylename = "headerstyle";
headerstyle.endupdate();
//autofit rows and columns
//sheet.usedrange.autofitrows();
sheet.usedrange.autofitcolumns();
sheet.range["a1"].text = title;
sheet.range["a1:" + constant[dt.columns.count - 1] + "1"].merge();
sheet.range["a1"].cellstyle.font.size = 10;
sheet.range["a1"].cellstyle.horizontalalignment = excelhalign.haligncenter;
//saving the workbook to disk.
workbook.saveas(httputility.urlencode(fname, encoding.utf8), excelsavetype.saveasxls, resp, exceldownloadtype.promptdialog);
//no exception will be thrown if there are unsaved workbooks.
excelengine.thrownotsavedondestroy = false;
excelengine.dispose();
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯