BOS运行时 - 引出 - 引出Excel支持币别原创
7人赞赏了该文章
232次浏览
编辑于2023年12月20日 17:56:03
列表引出时,支持了引出币别符号功能。标准平台是通过设置NPOI相关格式实现支持,以下案例演示引出excel支持币别符号。实现效果如下:
参考代码实现(仅NPOI设置格式部分)
private string NPOIExport(DataTable tb) { var wb = new SXSSFWorkbook(); SXSSFSheet sheet = (SXSSFSheet)wb.CreateSheet("demo"); var tempXmlFile = sheet._writer.TemporaryFilePath(); string fileName = string.Format("NPOI-demo-{0}.xlsx", DateTime.Now.ToString("yyyyMMddhhmmss")); var filePath = PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, fileName); var fileUrl = PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, PathUtils.UrlEncode(fileName)); try { for (int i = 0; i < tb.Rows.Count; i++) { IRow row = sheet.CreateRow(i); for (int colIndex = 0; colIndex < tb.Columns.Count; colIndex++) { ICell cell = row.CreateCell(colIndex); ICellStyle textCellStyle = wb.CreateCellStyle(); if (cell.ColumnIndex > 2 && i > 0) { var symbol = "$"; if(symbolDic.ContainsKey(i)) { symbol = symbolDic[i]; } textCellStyle.DataFormat = wb.CreateDataFormat().GetFormat(string.Format("{0}#,##0.00",symbol)); cell.SetCellValue(double.Parse(tb.Rows[i][colIndex].ToString())); } else { textCellStyle.DataFormat = wb.CreateDataFormat().GetFormat("@"); cell.SetCellValue(tb.Rows[i][colIndex].ToString()); } cell.CellStyle = textCellStyle; //设计公式 if (cell.ColumnIndex == 5 && i > 0) { cell.CellFormula = string.Format("D{0}*E{0}", i + 1); } if (i % 2 == 0) { textCellStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index; textCellStyle.FillPattern = FillPattern.SolidForeground; } } } //增加汇总行 IRow rowSummary = sheet.CreateRow(tb.Rows.Count); for (int colIndex = 0; colIndex < tb.Columns.Count; colIndex++) { //第一列汇总 ICell cell = rowSummary.CreateCell(colIndex); ICellStyle textCellStyle = wb.CreateCellStyle(); if (colIndex == 0) { textCellStyle.DataFormat = wb.CreateDataFormat().GetFormat("@"); cell.SetCellValue("合计"); } else if (cell.ColumnIndex > 2) { textCellStyle.DataFormat = wb.CreateDataFormat().GetFormat("#,##0.00"); cell.CellFormula = string.Format("SUM({0}{1}:{0}{2})", excelDic[colIndex], 1, tb.Rows.Count); } } //写入Excel using (FileStream fs = File.OpenWrite(filePath)) { wb.Write(fs); } } finally { if (wb != null) { wb.Close(); } //清理中间临时xml文件 if (File.Exists(tempXmlFile)) { File.Delete(tempXmlFile); } } return fileUrl; } private Dictionary<int, string> symbolDic = new Dictionary<int, string>() { {0,"$" }, {1,"¥" }, {2,"[$NTY]" }, };
赞 7
7人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!
推荐阅读