天天看点

【EPPlus使用】之导出Excel,设置单元格式

第一种 日期格式:            

cell.setCellValue(new Date());
 HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
 HSSFDataFormat format= demoWorkBook.createDataFormat();
 cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));
 cell.setCellStyle(cellStyle);
           

第二种 保留两位小数格式:

cell.setCellValue(1.2);
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00")); cell.setCellStyle(cellStyle);
           

第三种 货币格式            

cell.setCellValue(20000); 
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle(); 
HSSFDataFormat format= demoWorkBook.createDataFormat();
cellStyle.setDataFormat(format.getFormat("¥#,##0"));
cell.setCellStyle(cellStyle);
           

第四种 百分比格式:

cell.setCellValue(20);
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
cell.setCellStyle(cellStyle);
           

  此种情况跟第二种一样

第五种:中文大写格式            

cell.setCellValue(20000);
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
HSSFDataFormat format= demoWorkBook.createDataFormat();
cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));
cell.setCellStyle(cellStyle);
           

第六种:科学计数法格式             

cell.setCellValue(20000);
HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();
cellStyle.setDataFormat( HSSFDataFormat.getBuiltinFormat("0.00E+00"));
cell.setCellStyle(cellStyle);
           

选定区间设置字符串格式或数字格式:

Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range(worksheet.Cells[rowIndex, 1], worksheet.Cells[rowCount+rowIndex-1, columnCount-1]);
range.NumberFormat = "@";//设置数字文本格式
Microsoft.Office.Interop.Excel.Range rangeinfo = worksheet.get_Range(worksheet.Cells[rowIndex, 4], worksheet.Cells[rowCount + rowIndex - 1, 4]);
rangeinfo.NumberFormat = "00";
           

其他:

//Excel单元格格式设置 :
worksheet.Cells[row,colum].Style.Numberformat.Format = "@";单元格类型设置为文本类型
worksheet.Column(colum).Style.Numberformat.Format="@"设置列类型为文本类型

@"yyyy-mm-dd";//日期型格式

 

worksheet.Cells[1,1].Style.Font.Name="正楷";//设置字体

worksheet.Cells[1,1].Style.Font.Size=14;//设置字体大小

worksheet.Row(row).Style.Numberformat.Format="@"设置行类型为文本类型
worksheet.Cells[""].Style.Font.Color.Indexed=5 //设置单元格中字体的颜色 
//或者 
worksheet.Cells[1, 1].Style.Font.Color.SetColor(Color.White);
下图为颜色对应的数字
           

其他解决方案:c# – 使用EPPlus如何生成电子表格,其中数字是数字而不是文本

Q: 我正在从List< object []>创建电子表格.使用LoadFromArrays

数组的第一个条目是标题,其他条目可能是数字,文本或日期(但列表中的每个数组都相同).

生成的Excel工作表具有绿色三角形警告,表示数字被格式化为文本.

我循环遍历所有单元格并将其格式设置为数字,如此ws.Cells [i,j] .Style.Numberformat.Format =“0”;

然而问题仍然存在,我仍然看到绿色警告,即使我在查看格式单元格…对话框中将数字格式设置为数字.

我有什么选择?我可以更多地了解每列中的类型,但是如何设置列标题?

有比EPPlus更好的解决方案吗?或者在下载之前我可以做一些电子表格的后期处理?

S:由于您使用的是对象数组,因此它们可以包含看起来像数字的数字和字符串,您必须遍历每个对象并确定其类型:

[TestMethod]
public void Object_Type_Write_Test()
{
    //http://stackoverflow.com/questions/31537981/using-epplus-how-can-i-generate-a-spreadsheet-where-numbers-are-numbers-not-text
    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    //Some data
    var list = new List<Object[]>
    {
        new object[]
        {
            "111.11",
            111.11,
            DateTime.Now
        }
    };

    using (var package = new ExcelPackage(existingFile))
    {
        var ws = package.Workbook.Worksheets.Add("Sheet1");
        ws.Cells[1, 1, 2, 2].Style.Numberformat.Format = "0";
        ws.Cells[1, 3, 2, 3].Style.Numberformat.Format = "[$-F400]h:mm:ss\\ AM/PM";

        //This will cause numbers in string to be stored as string in excel regardless of cell format
        ws.Cells["A1"].LoadFromArrays(list);

        //Have to go through the objects to deal with numbers as strings
        for (var i = 0; i < list.Count; i++)
        {
            for (var j = 0; j < list[i].Count(); j++)
            {

                if (list[i][j] is string)
                    ws.Cells[i + 2, j + 1].Value = Double.Parse((string) list[i][j]);
                else if (list[i][j] is double)
                    ws.Cells[i + 2, j + 1].Value = (double)list[i][j];
                else
                    ws.Cells[i + 2, j + 1].Value = list[i][j];

            }
        }

        package.Save();
    }
}
           

参考文章:

http://www.voidcn.com/article/p-rnkjehei-btk.html

https://www.cnblogs.com/liujinyuan/p/3267526.html

https://www.cnblogs.com/siyunianhua/p/6852750.html

https://www.cnblogs.com/Caocaodemo/p/10415901.html

https://blog.csdn.net/weixin_30737433/article/details/101401834

https://jingyan.baidu.com/article/0f5fb099bbec982d8334eae4.html