//using Aspose.Cells; 下载aspose.dll public static void createExcel() { Workbook wb = new Workbook(); Worksheet sheet = wb.Worksheets[0]; //设置样式 Style style = wb.CreateStyle(); style.ForegroundColor = System.Drawing.Color.FromArgb(199, 214, 157); style.HorizontalAlignment = TextAlignmentType.Center; style.Pattern = BackgroundType.Solid; //绑定数据 sheet.Cells[0, 0].PutValue("工号1"); sheet.Cells[0, 1].PutValue("姓名"); //绑定样式 sheet.Cells[0, 0].SetStyle(style); sheet.Cells[0, 1].SetStyle(style); wb.Save(@"F:/期间模板1.xlsx"); }
//MVC 控制器返回文件流,设置响应头
//方法返回文件流 public static MemoryStream createExcel() { Workbook wb = new Workbook(); Worksheet sheet = wb.Worksheets[0]; //设置样式 Style style = wb.CreateStyle(); style.ForegroundColor = System.Drawing.Color.FromArgb(199, 214, 157); style.HorizontalAlignment = TextAlignmentType.Center; style.Pattern = BackgroundType.Solid; //绑定数据 sheet.Cells[0, 0].PutValue("工号1"); sheet.Cells[0, 1].PutValue("姓名"); //绑定样式 sheet.Cells[0, 0].SetStyle(style); sheet.Cells[0, 1].SetStyle(style); return wb.SaveToStream(); } //控制器设置响应头 接收文件流 var excel = createExcel(); Response.ContentType = "application/vnd.ms-excel"; Response.Charset = "utf-8"; Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "排班模板.xls")); Response.Clear(); excel.WriteTo(Response.OutputStream); Response.End();
//excel设置列格式为文本样式 Style st = wb.CreateStyle(); st.Number = 49; StyleFlag flag = new StyleFlag(); flag.NumberFormat = true; sheet.Cells.Columns[0].ApplyStyle(st, flag);
Aspose:DownLoad
来源:https://www.cnblogs.com/tangpeng97/p/7850243.html