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

更新时间:2023-07-20 20:38:05 阅读: 评论:0

【EPPlus使⽤】之导出Excel,设置单元格式第⼀种 ⽇期格式:
cell.tCellValue(new Date());
HSSFCellStyle cellStyle = ateCellStyle();
HSSFDataFormat format= ateDataFormat();
cellStyle.Format("yyyy年m⽉d⽇"));
cell.tCellStyle(cellStyle);
第⼆种 保留两位⼩数格式:
cell.tCellValue(1.2);
HSSFCellStyle cellStyle = ateCellStyle();
cellStyle.BuiltinFormat("0.00")); cell.tCellStyle(cellStyle);
第三种 货币格式
cell.tCellValue(20000);
HSSFCellStyle cellStyle = ateCellStyle();
HSSFDataFormat format= ateDataFormat();
北汉cellStyle.Format("¥#,##0"));
老太太歇后语cell.tCellStyle(cellStyle);
第四种 百分⽐格式:
cell.tCellValue(20);
HSSFCellStyle cellStyle = ateCellStyle();
cellStyle.BuiltinFormat("0.00%"));
cell.tCellStyle(cellStyle);
此种情况跟第⼆种⼀样
第五种:中⽂⼤写格式
cell.tCellValue(20000);
HSSFCellStyle cellStyle = ateCellStyle();
HSSFDataFormat format= ateDataFormat();
cellStyle.Format("[DbNum2][$-804]0"));
cell.tCellStyle(cellStyle);
第六种:科学计数法格式
cell.tCellValue(20000);
HSSFCellStyle cellStyle = ateCellStyle();
cellStyle.tDataFormat( BuiltinFormat("0.00E+00"));
cell.tCellStyle(cellStyle);
选定区间设置字符串格式或数字格式:
Microsoft.Office.Interop.Excel.Range range = _Range(worksheet.Cells[rowIndex, 1], worksheet.Cells[rowCount+rowIndex-1, columnCount-1 range.NumberFormat = "@";//设置数字⽂本格式
我心中的玫瑰Microsoft.Office.Interop.Excel.Range rangeinfo = _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()
{
///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 cau 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.Par((string) list[i][j]);
el if (list[i][j] is double)
ws.Cells[i + 2, j + 1].Value = (double)list[i][j];
el
ws.Cells[i + 2, j + 1].Value = list[i][j];
}
}
package.Save();
}
}
参考⽂章:

本文发布于:2023-07-20 20:38:05,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1089587.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数字   格式   设置   类型
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图