C# NPOI 操作Excel 案例

时间:2023-03-08 15:39:53
C# NPOI 操作Excel 案例

1、加入NPOI 程序集,使用nuget添加程序集

2、引用NPOI程序集

        private IWorkbook ExportExcel(PrintQuotationOrderViewModel model)
{
//if (model == null) return string.Empty;
string tempDirPath = Server.MapPath("/Templates/Excel/");
if (!Directory.Exists(tempDirPath))
{
Directory.CreateDirectory(tempDirPath);
}
IWorkbook workbook;
string excelTempPath = tempDirPath + "quotaExcelTemp-new.xls";
//加载excel模板
using (FileStream fs = new FileStream(excelTempPath, FileMode.Open, FileAccess.Read))
{
//XSSFWorkbook 适用XLSX格式,HSSFWorkbook 适用XLS格式
workbook = new HSSFWorkbook(fs);
} ISheet sheet = workbook.GetSheetAt();
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.QuotedOn.ToString("yyyy-MM-dd")); sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Number); sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.CustomerPurchaseNumber);
//甲方
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Buyer.Company.Name);
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Buyer.Name);
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Buyer.Email);
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Receiver.Mobile);
sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Receiver.Address); //乙方
sheet.GetRow().GetCell().SetCellValue("XXXXX有限公司");
ICellStyle cstyle = workbook.CreateCellStyle();
cstyle.Alignment = HorizontalAlignment.Left;
sheet.GetRow().GetCell().CellStyle = cstyle; sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.SalesmanName);
sheet.GetRow().GetCell().CellStyle = cstyle; sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Salesman.Mobile); sheet.GetRow().GetCell().CellStyle = cstyle; sheet.GetRow().GetCell().SetCellValue(model.QuotationOrder.Salesman.Email);
sheet.GetRow().GetCell().CellStyle = cstyle; int count = model.QuotationItems.Count;
for (int i = ; i < count; i++)
{ //设置列头的单元格样式
HSSFCellStyle cellStyle = workbook.CreateCellStyle() as HSSFCellStyle; IRow row = sheet.CopyRow(, + i);
ICell cell = row.CreateCell();
cell.SetCellValue((i + ));
ICellStyle style1 = SetCellStyle((HSSFWorkbook)workbook, HorizontalAlignment.Left);
cell.CellStyle = style1; cell = row.CreateCell();
cell.SetCellValue(model.QuotationItems[i].Product.Name);
cell.CellStyle = style1; cell = row.CreateCell();
cell.CellStyle = style1;
//合并单元格
CellRangeAddress region = new CellRangeAddress( + i, + i, , );
sheet.AddMergedRegion(region); cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].CustomCode);
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Product.Code);
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue("PCS");
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Quantity);
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Quotation.DispatchDays >= ? ((int)model.QuotationItems[i].Quotation.DispatchDays).ToString() : "");
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Quotation.UnitPriceWithTax >= ? ((decimal)model.QuotationItems[i].Quotation.UnitPriceWithTax).ToString("f2") : "");
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Quotation.SubtotalWithTax.ToString("f2"));
cell = row.CreateCell();
cell.CellStyle = style1;
cell.SetCellValue(model.QuotationItems[i].Remark);
} sheet.GetRow( + count).GetCell().SetCellValue(model.QuotationOrder.Shipping.Amount.ToString("f2"));
sheet.GetRow( + count).GetCell().SetCellValue(model.QuotationOrder.TotalWithTax.ToString("f2"));
sheet.GetRow( + count).GetCell().SetCellValue(model.QuotationOrder.TotalWithTaxInChinese);
sheet.GetRow( + count).GetCell().SetCellValue(model.Payment); return workbook;
}

3、设置表格样式

  /// <summary>
/// 给Excel添加边框
/// </summary>
private ICellStyle SetCellStyle(HSSFWorkbook hssfworkbook, HorizontalAlignment ha)
{
ICellStyle cellstyle = hssfworkbook.CreateCellStyle();
cellstyle.Alignment = ha; //有边框
cellstyle.BorderBottom = BorderStyle.Thin;
cellstyle.BorderLeft = BorderStyle.Thin;
cellstyle.BorderRight = BorderStyle.Thin;
cellstyle.BorderTop = BorderStyle.Thin;
return cellstyle;
}

4、excel加载图片

  HSSFPatriarch patriarch = (HSSFPatriarch)sheet.DrawingPatriarch;
HSSFClientAnchor anchor = new HSSFClientAnchor(, , , , , + count, , + count);
HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage(tempDirPath + "1.png", (HSSFWorkbook)workbook));

LoadImage 方法

    private int LoadImage(string path, HSSFWorkbook wb)
{
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[file.Length];
file.Read(buffer, , (int)file.Length);
return wb.AddPicture(buffer, PictureType.PNG); }

5、导出excel

var stream = new MemoryStream();
var work = ExportExcel(printQuotationOrderViewModel);
work.Write(stream);
//mvc代码
return File(stream.GetBuffer(), "application/vnd.ms-excel", quotedOrderModel.Number + ".xls");