原文網址 https://www.cnblogs.com/love-zf/p/7450409.html
BorderStyle.Thin 全稱爲 NPOI.SS.UserModel.BorderStyle.Thin
最後那個關於報錯的修改,親測有效.
網上找到了,HSSFWorkbook自定義顏色的例子(講的還挺細緻的),但是XSSFWorkbook確沒找到...研究了一下,坑掉了一地...
NPOI.XSSF.UserModel.XSSFWorkbook xssfworkbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
方案壹:>>XSSFCellStyle
XSSFCellStyle rowsStyleColor = (XSSFCellStyle)xssfworkbook.CreateCellStyle(); XSSFColor xssfColor = new XSSFColor(); //根據自己需要設置RGB byte[] colorRgb = { (byte)252, (byte)139, (byte)139 }; xssfColor.SetRgb(colorRgb); rowsStyleColor.FillForegroundColorColor = xssfColor; rowsStyleColor.FillPattern = FillPattern.SolidForeground;
方案貳:>>ICellStyle
ICellStyle rowsStyleColor = (XSSFCellStyle)xssfworkbook.CreateCellStyle(); rowsStyleColor.Alignment = HorizontalAlignment.Center; rowsStyleColor.VerticalAlignment = VerticalAlignment.Center; rowsStyleColor.BorderBottom = BorderStyle.Thin; rowsStyleColor.BorderLeft = BorderStyle.Thin; rowsStyleColor.BorderRight = BorderStyle.Thin; rowsStyleColor.BorderTop = BorderStyle.Thin; rowsStyleColor.WrapText = true; //設置背景顏色... rowsStyleColor.FillForegroundColor = 0; rowsStyleColor.FillPattern = FillPattern.SolidForeground; ((XSSFColor)rowsStyleColor.FillForegroundColorColor).SetRgb(new byte[] { 252, 139, 139 });
感覺挺彆扭的...一點搞不好,就是錯誤...
其他的報錯:
//****************************文件流... public ActionResult ExportExcelFile() { byte[] streamData = null; try { MemoryStream file = new MemoryStream(); workbook.Write(file); /*這個錯誤導致我一直以爲NPOI不能導出不報錯的.xlsx類型的Excel呢!!!···*/ streamData = file.ToArray();//這種寫法就可以的... //streamData =file.GetBuffer();//用這麼的寫法,導出的Excel,就是報錯,類似格式不正確,需要修復才能打開... if (streamData == null || streamData.Length == 0) { return Content("無數據供導出。"); } Response.Charset = "UTF-8"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "filename=Test.xlsx"); Response.AddHeader("Content-Length", streamData.LongLength.ToString()); Response.BinaryWrite(streamData); Response.Flush(); Response.End(); return null; } catch (Exception ex) { return Content("未知錯誤>>>"); } }