C# 用openxml做excel报表怎么做。excel有模板。查询的数据填充到模板指定的单元格内
发布网友
发布时间:2022-05-26 11:53
我来回答
共2个回答
热心网友
时间:2023-10-15 20:29
如果EXCEL03格式的,用NPOI
public string GenerationToExcel(string path,string tempPath)
{
HSSFWorkbook workBook = new HSSFWorkbook(new FileStream(@tempPath, FileMode.Open, FileAccess.Read));
HSSFSheet sheet = workBook.GetSheet("Detail");
HSSFRow row = null;
List<LogisticsReportEntity> easCes = DataFacade<DLogictiscReport>.CreateProvider().getLogisticsReport();
int rowNum = 0;
HSSFCell cell =null; ;
HSSFCellStyle dateTimeCellStyle = workBook.CreateCellStyle();
HSSFDataFormat format = workBook.CreateDataFormat();
dateTimeCellStyle.DataFormat = format.GetFormat("mm/dd/yy");
foreach (LogisticsReportEntity o in easCes)
{
rowNum++;
row = sheet.CreateRow(rowNum);
int cNum = 0;
row.CreateCell(cNum++).SetCellValue(o.Status);
row.CreateCell(cNum++).SetCellValue(o.ReasonCode);
row.CreateCell(cNum++).SetCellValue(o.CustomerCode);
row.CreateCell(cNum++).SetCellValue(o.CustomerName);
row.CreateCell(cNum++).SetCellValue(o.TranNum);
row.CreateCell(cNum++).SetCellValue(o.CustRef);
}
string tempFileId = DateTime.Now.ToString("yyyyMMddHHmmss") + WebCom.RandomnAlpha(4);
FileStream file = new FileStream(path + tempFileId + ".xls", FileMode.Create);
workBook.Write(file);
file.Close();
return tempFileId;
热心网友
时间:2023-10-15 20:30
建议引用SpreadsheetGear.dll来做,这个是专门做excel表格的,很好很强大。
有了模板之后,不管你数据源是xml还是什么,用这里面的方法和接口 直接往文档里面填就ok了