第一種 日期格式:
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