记录一下在项目中需要用到EXCEL导出功能,详细教程
在本文的开始你需要在UI层添加两个.DLL
ICSharpCode.SharpZipLib.dll 和 NPOI.dll
首先在model层添加类
namespace YM.Model.CustomModel
{
public class JiJiaModel
{
public int ID { get; set; }
public string PartCode { get; set; }//总件号
public string TaskNum { get; set; }//任务号
public string PartCodeSon { get; set; }//子件号
public string SingleCount { get; set; }//单台数量
public string StockCount { get; set; }//
public string AddCount { get; set; }//加工数量
public string FormatDate { get; set; }//要求日期
public string Beizhu { get; set; }//属于哪个总件
//C.FPartName,C.FPartSize,C.FBlankSize,C.FMaterialSpecification,c.FPartUnit
public string FPartName { get; set; }//子件的零件名称
public string FPartSize { get; set; }//零件规格
public string FBlankSize { get; set; }//毛胚尺寸
public string FMaterialSpecification { get; set; }//零件材质
public string FPartUnit { get; set; }//单位
}
}
然后在BLL层完成与数据库的交互
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YM.DAL;
using YM.Model.CustomModel;
namespace YM.BLL.Tables
{
public class JiJia
{
public List<JiJiaModel> GetJiJiaInfos()
{
string sql =
"select ID,TaskNum,PartCode,PartCodeSon,FPartName,FPartUnit,SingleCount,AddCount,FMaterialSpecification,FPartSize,FBlankSize,FormatDate,Beizhu from t_MachiningList2 ; ";
List<JiJiaModel> list = new List<JiJiaModel>();
DataTable dt = SqlHelper.GetTable(sql, CommandType.Text);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
JiJiaModel jijiaInfo = new JiJiaModel();
RowToModel(dt.Rows[i], jijiaInfo);
list.Add(jijiaInfo);
}
}
return list;
}
private void RowToModel(DataRow dataRow, JiJiaModel jijiaInfo)
{
jijiaInfo.PartCode = dataRow["PartCode"] == DBNull.Value ? "" : dataRow["PartCode"].ToString();
jijiaInfo.TaskNum = dataRow["TaskNum"] == DBNull.Value ? "" : dataRow["TaskNum"].ToString();
jijiaInfo.PartCodeSon = dataRow["PartCodeSon"] == DBNull.Value ? "" : dataRow["PartCodeSon"].ToString();
jijiaInfo.SingleCount = dataRow["SingleCount"] == DBNull.Value ? "" : dataRow["SingleCount"].ToString();
jijiaInfo.AddCount = dataRow["AddCount"] == DBNull.Value ? "" : dataRow["AddCount"].ToString();
jijiaInfo.FormatDate = dataRow["FormatDate"] == DBNull.Value ? "" : dataRow["FormatDate"].ToString();
jijiaInfo.Beizhu = dataRow["Beizhu"] == DBNull.Value ? "" : dataRow["Beizhu"].ToString();
//C.FPartName,C.FPartSize,C.FBlankSize,C.FMaterialSpecification,c.FPartUnit
jijiaInfo.FPartName = dataRow["FPartName"] == DBNull.Value ? "" : dataRow["FPartName"].ToString();
jijiaInfo.FPartSize = dataRow["FPartSize"] == DBNull.Value ? "" : dataRow["FPartSize"].ToString();
jijiaInfo.FBlankSize = dataRow["FBlankSize"] == DBNull.Value ? "" : dataRow["FBlankSize"].ToString();
jijiaInfo.FMaterialSpecification = dataRow["FMaterialSpecification"] == DBNull.Value ? "" : dataRow["FMaterialSpecification"].ToString();
jijiaInfo.FPartUnit = dataRow["FPartUnit"] == DBNull.Value ? "" : dataRow["FPartUnit"].ToString();
}
}
}
然后在控制器里写 ExportToExcel方法
public FileResult ExportToExcel()
{
//创建Excel文件的对象
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//添加一个sheet
NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
//获取list数据
List<JiJiaModel> list = new JiJia().GetJiJiaInfos();
//给sheet1添加第一行的头部标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
row1.CreateCell(0).SetCellValue("序号");
row1.CreateCell(1).SetCellValue("任务号");
row1.CreateCell(2).SetCellValue("总件号");
row1.CreateCell(3).SetCellValue("子件号");
row1.CreateCell(4).SetCellValue("零件名称");
row1.CreateCell(5).SetCellValue("单位");
row1.CreateCell(6).SetCellValue("单套数量");
row1.CreateCell(7).SetCellValue("加工数量");
row1.CreateCell(8).SetCellValue("材质");
row1.CreateCell(8).SetCellValue("规格");
row1.CreateCell(9).SetCellValue("毛坯尺寸");
row1.CreateCell(10).SetCellValue("要求日期");
row1.CreateCell(11).SetCellValue("备注");
//将数据逐步写入sheet1各个行
for (int i = 0; i < list.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
rowtemp.CreateCell(0).SetCellValue(list[i].ID.ToString());
rowtemp.CreateCell(1).SetCellValue(list[i].TaskNum.ToString());
rowtemp.CreateCell(2).SetCellValue(list[i].PartCode.ToString());
rowtemp.CreateCell(3).SetCellValue(list[i].PartCodeSon.ToString());
rowtemp.CreateCell(4).SetCellValue(list[i].FPartUnit.ToString());
rowtemp.CreateCell(5).SetCellValue(list[i].SingleCount.ToString());
rowtemp.CreateCell(6).SetCellValue(list[i].AddCount.ToString());
rowtemp.CreateCell(7).SetCellValue(list[i].FormatDate.ToString());
rowtemp.CreateCell(8).SetCellValue(list[i].FMaterialSpecification.ToString());
rowtemp.CreateCell(9).SetCellValue(list[i].FPartSize.ToString());
rowtemp.CreateCell(10).SetCellValue(list[i].FBlankSize.ToString());
rowtemp.CreateCell(11).SetCellValue(list[i].Beizhu.ToString());
}
// 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application/vnd.ms-excel", "清单列表.xls");
}
Views中代码如下
@Html.ActionLink("导出Excel", "ExportToExcel", new { @class = "btn btn-default" })
DLL下载地址 密码:53xc