利用npoi导出Excel

时间:2021-07-20 14:02:09

npoi库是当下最流行的处理Excel、Word、PPT等Office文件格式

npoi的下载地址:http://npoi.codeplex.com/
npoi的官方学习地址: http://www.npoi.info/page/3
今天遇到一个需求,就是把下面的数据结构导出成Excel文件,不说了,先上数据模型(下面的模型仅仅是从实际模型中抽离出来为演示使用的)如下所示:
/// <summary>
/// 线下活动商品模型
/// </summary>
public class OffLineCart
{
/// <summary>
///
/// </summary>
public OffLineCart()
{
}
/// <summary>
/// 商品Id
/// </summary>
public string ProductId { get; set; }
/// <summary>
/// SKUId
/// </summary>
public string SkuId { get; set; }
/// <summary>
/// 商品现价
/// </summary>
public decimal Price { get; set; }
/// <summary>
/// 商品数量
/// </summary>
public int Qty { get; set; }
}
/// <summary>
/// 线下活动提交订单集合
/// </summary>
public class OffLineOrderListModel
{
public OffLineOrderListModel()
{
}
/// <summary>
/// 子订单集合
/// </summary>
public IList<OffLineCart> OffLineCartList { get; set; }
/// <summary>
/// 子订单集合总价
/// </summary>
public decimal TotalPrice {
get
{
if (OffLineCartList.Count > 0)
{
var totalprice = OffLineCartList.Sum(q => q.Price * q.Qty);
return totalprice;
}
return 0;
}
}
/// <summary>
/// 姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 手机号
/// </summary>
public string Phone { get; set; }
/// <summary>
/// 地推码
/// </summary>
public string Code { get; set; }
/// <summary>
/// 收货地址
/// </summary>
public string Address { get; set; }
/// <summary>
/// 邮箱
/// </summary>
public string Email { get; set; }
/// <summary>
/// 订单号
/// </summary>
public string OrderNo { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CreateTime { get; set; }
需求是这样的就是把OffLineOrderListModel(订单模型)中的OffLineCart(订单商品详情)一行行的导出到Excel,而OffLineOrderListModel其他的字段中导出一行就行了,说白了就是OffLineOrderListModel的其他字段合并单元格就可以了;
因为一直以前导出Excel一直是使用EPPlus(http://www.cnblogs.com/liudeyun/p/3535740.html),我也是第一次使用npoi操作,发现一般都是通过DataTable导出的,我的数据格式是List集合,需要通过装换才可以导出,觉得麻烦,于是就写了,主要的导出代码如下:
/// <summary>
/// 导出对应的订单集合
/// </summary>
/// <param name="offLineOrderListModelList"></param>
/// <param name="strHeaderText">表头文本</param>
public static MemoryStream DataTableToExcel(List<OffLineOrderListModel> offLineOrderListModelList , string strHeaderText)
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();
//excel总共有11列
string[] columnarr = new string[]
{
"收货人姓名",
"收货人手机",
"收货人邮箱",
"收货人地址",
"销售人员编码",
"订单号",
"订单生成时间",
"地推版本号", //订单商品拆分,其余的订单信息单元格合并
"订单商品Id",
"订单商品SKUId",
"订单商品数量",
"订单商品单价", "订单总价"
};
HSSFRow firstRow = (HSSFRow)sheet.CreateRow(0);
firstRow.Height = 25*25;
//头标颜色标识
for (int i = 0; i < columnarr.Length; i++)
{
firstRow.CreateCell(i).CellStyle = GetCellStyle(workbook);
firstRow.CreateCell(i).SetCellValue(columnarr[i]);
} #region 设置各列的宽度 sheet.SetColumnWidth(0, 15 * 256);
sheet.SetColumnWidth(1, 15 * 256);
sheet.SetColumnWidth(2, 25 * 256);
sheet.SetColumnWidth(3, 30 * 256);
sheet.SetColumnWidth(4, 15 * 256);
sheet.SetColumnWidth(5, 25 * 256);
sheet.SetColumnWidth(6, 25 * 256);
sheet.SetColumnWidth(7, 25 * 256);
sheet.SetColumnWidth(8, 35 * 256);
sheet.SetColumnWidth(9, 35 * 256);
sheet.SetColumnWidth(10, 15 * 256);
sheet.SetColumnWidth(11, 15 * 256);
sheet.SetColumnWidth(12, 15 * 256); #endregion int rowIndex = 1;
foreach (OffLineOrderListModel offLineOrderListModel in offLineOrderListModelList)
{
foreach (var offLineCart in offLineOrderListModel.OffLineCartList)
{
int col = 0; HSSFRow aotherRow = (HSSFRow)sheet.CreateRow(rowIndex);
aotherRow.Height = 22*22;
aotherRow.CreateCell(col).SetCellValue(offLineOrderListModel.Name);
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineOrderListModel.Phone);
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineOrderListModel.Email);
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineOrderListModel.Address);
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineOrderListModel.Code);
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineOrderListModel.OrderNo);
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineOrderListModel.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineOrderListModel.Version);
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineCart.ProductId);
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineCart.SkuId);
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineCart.Qty);
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineCart.Price.ToString("N"));
col++;
sheet.GetRow(rowIndex).CreateCell(col).SetCellValue(offLineOrderListModel.TotalPrice.ToString("N"));
rowIndex++; }
//合并订单基本信息单元格
//要合并的单元格
string[] mergedCellStringArr = new[]
{
"收货人姓名",
"收货人手机",
"收货人邮箱",
"收货人地址",
"销售人员编码",
"订单号",
"订单生成时间",
"地推版本号"
};
for (int i = 0; i < mergedCellStringArr.Length; i++)
{ Region region = new Region(rowIndex-offLineOrderListModel.OffLineCartList.Count, i, rowIndex-1, i);
sheet.AddMergedRegion(region);
}
//合并订单总价单元格
sheet.AddMergedRegion(new Region(rowIndex - offLineOrderListModel.OffLineCartList.Count, columnarr.Length-1, rowIndex - 1, columnarr.Length-1)); }
using (MemoryStream ms = new MemoryStream())
{
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
//为什么会无法释放呢
//sheet.Dispose();
//workbook.Dispose();
workbook = null;
return ms;
}
}

  

最后的结果就是这样的:
利用npoi导出Excel
利用npoi导出Excel
只不过合并单元格后,设置了单元格水平和垂直居中,没有反应!
由于我是菜鸟,可能描述的不好,请大神们见谅,也请大神们批评指正,谢谢!