BOS运行时 - 引出 - 引出Excel支持币别原创
金蝶云社区-Howhy
Howhy
7人赞赏了该文章 147次浏览 未经作者许可,禁止转载编辑于2023年12月20日 17:56:03

列表引出时,支持了引出币别符号功能。标准平台是通过设置NPOI相关格式实现支持,以下案例演示引出excel支持币别符号。实现效果如下:

image.png

参考代码实现(仅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