Aspose.Cells导出Excel(1)
2018-06-17 22:25:24来源:未知 阅读 ()
利用Aspose.Cells导出excel
注意的问题
1、DataTable的处理
2、进行编码,便于中文名文件下载
3、别忘了Aspose.Cells.dll(可以自己在网上搜索)
public static bool DataTableToExcel2(DataTable datatable, string filepath, out string error) { error = ""; Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(); try { if (datatable == null) { error = "DataTableToExcel:datatable 为空"; return false; } //为单元格添加样式 Aspose.Cells.Style style = wb.Styles[wb.Styles.Add()]; //设置居中 style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center; //设置背景颜色 style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); style.Pattern = BackgroundType.Solid; style.Font.IsBold = true; int rowIndex = 0; for (int i = 0; i < datatable.Columns.Count; i++) { DataColumn col = datatable.Columns[i]; string columnName = col.Caption ?? col.ColumnName; wb.Worksheets[0].Cells[rowIndex, i].PutValue(columnName); wb.Worksheets[0].Cells[rowIndex, i].SetStyle(style); } rowIndex++; foreach (DataRow row in datatable.Rows) { for (int i = 0; i < datatable.Columns.Count; i++) { wb.Worksheets[0].Cells[rowIndex, i].PutValue(row[i].ToString()); } rowIndex++; } for (int k = 0; k < datatable.Columns.Count; k++) { wb.Worksheets[0].AutoFitColumn(k, 0, 150); } wb.Worksheets[0].FreezePanes(1, 0, 1, datatable.Columns.Count); wb.Save(filepath); return true; } catch (Exception e) { error = error + " DataTableToExcel: " + e.Message; return false; } } protected void btnExport_Click(object sender, EventArgs e) {//导出 int ClassID = 0; int.TryParse(hidClassID.Value, out ClassID); string error = ""; string filepath = ""; BLL.TUser bll_TUser = new BLL.TUser(); BLL.TClass bll_Class = new BLL.TClass(); Model.TClass model = (new BLL.TClass()).GetModel(ClassID); //处理DataTable DataTable dt = bll_TUser.GetListByClass(ClassID); DataTable dtNew = new DataTable(); dtNew.Columns.Add("姓名", typeof(string)); dtNew.Columns.Add("学号", typeof(string)); dtNew.Columns.Add("性别", typeof(string)); dtNew.Columns.Add("电话", typeof(string)); if (dt != null && dt.Rows.Count > 0) { DataRow drNew = dtNew.NewRow(); foreach (DataRow dr in dt.Rows) { //drNew = dtNew.NewRow(); drNew["姓名"] = dr["UserName"]; drNew["学号"] = dr["IDNO"]; drNew["性别"] = dr["Sex"].ToString() == "1" ? "男" : (dr["Sex"].ToString() == "2" ? "女" : ""); drNew["电话"] = dr["Phone"]; dtNew.Rows.Add(drNew.ItemArray); } } if (model != null) { filepath = "/UploadFiles/ExportClass/";// + model.ClassName + ".xlsx"; string filaname = model.ClassName + ".xlsx"; string finalPath = MapPath("~" + filepath + filaname); //检查有该路径是否就创建 if (!Directory.Exists(MapPath("~/UploadFiles/ExportClass/"))) { Directory.CreateDirectory(MapPath("~/UploadFiles/ExportClass/")); } if (DataTableToExcel2(dtNew, finalPath, out error)) { string SiteRoot = "http://" + Request.Url.Authority.ToString() + filepath + Uri.EscapeDataString(filaname); //进行编码,便于中文名文件下载 //下载excel ClientScript.RegisterStartupScript(this.GetType(), "", ",<script type='text/javascript'>window.open('" + SiteRoot + "');</script>"); } else { ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('提示', '" + error + "!');</script>"); } } else { ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('提示', '班级不存在!');</script>"); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Python sklearn拆分训练集、测试集及预测导出评分 决策树 2019-08-13
- Python openpyxl : Excel 文档简单操作 2019-04-25
- Python-接口自动化(九) 2019-04-25
- Python虚拟环境包导出 2019-04-25
- Excel文件读取的两种方式 2019-04-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash