Npoi导出xlsx

时间:2023-03-09 15:51:13
Npoi导出xlsx

xlsx文件可以超出,excel2003行数限制。必要的dll文件。

Npoi导出xlsx

        public void Export()
{
string filename = Request["searchString"]; Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename+ ".xlsx")); NPOI.XSSF.UserModel.XSSFWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet1 = workbook.CreateSheet("BOM详情"); //给sheet1添加第一行的头部标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow();
row1.CreateCell().SetCellValue("物料编码");
row1.CreateCell().SetCellValue("物料名称");
row1.CreateCell().SetCellValue("规格型号");
row1.CreateCell().SetCellValue("物料用量");
row1.CreateCell().SetCellValue("用量单位");
row1.CreateCell().SetCellValue("备注");
//将数据逐步写入sheet1各个行
List<AkBom> pageResult = _akBomRepository.GetPageList(, , Request["searchString"], "");
for (int i = ; i < pageResult.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + );
rowtemp.CreateCell().SetCellValue(pageResult[i].ChildNumber);
rowtemp.CreateCell().SetCellValue(pageResult[i].ChildName);
rowtemp.CreateCell().SetCellValue(pageResult[i].Spec);
rowtemp.CreateCell().SetCellValue(double.Parse(pageResult[i].MaterialSum.ToString()));
rowtemp.CreateCell().SetCellValue(pageResult[i].Unit);
rowtemp.CreateCell().SetCellValue(pageResult[i].Remark);
}
//写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
workbook.Write(ms);
Response.BinaryWrite(ms.ToArray()); Response.Flush();
Response.End();
}
public void Export()
{
string searchString = Request["searchString"];
string line = Request["line"];
string station = Request["station"];
string begin = Request["begin"];
string end = Request["end"]; Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "FQC.xlsx")); NPOI.XSSF.UserModel.XSSFWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet1 = workbook.CreateSheet("FQC"); //excel格式化
NPOI.SS.UserModel.ICellStyle dateStyle = workbook.CreateCellStyle();
dateStyle.DataFormat = workbook.CreateDataFormat().GetFormat("yyyy/m/d h:mm:ss"); NPOI.SS.UserModel.ICellStyle numberStyle = workbook.CreateCellStyle();
numberStyle.DataFormat = workbook.CreateDataFormat().GetFormat("0.00000"); NPOI.SS.UserModel.ICellStyle textStyle = workbook.CreateCellStyle();
textStyle.DataFormat = workbook.CreateDataFormat().GetFormat("@"); //给sheet1添加第一行的头部标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow();
row1.CreateCell().SetCellValue("订单号");
row1.CreateCell().SetCellValue("条码");
row1.CreateCell().SetCellValue("档位名称");
row1.CreateCell().SetCellValue("Pmax");
row1.CreateCell().SetCellValue("功率档");
row1.CreateCell().SetCellValue("功率档范围");
row1.CreateCell().SetCellValue("Ipm");
row1.CreateCell().SetCellValue("电流档");
row1.CreateCell().SetCellValue("电流档范围");
row1.CreateCell().SetCellValue("规格");
row1.CreateCell().SetCellValue("产品等级");
row1.CreateCell().SetCellValue("电池片等级");
row1.CreateCell().SetCellValue("FQC不良");
row1.CreateCell().SetCellValue("判定结果");
row1.CreateCell().SetCellValue("人员");
row1.CreateCell().SetCellValue("线别");
row1.CreateCell().SetCellValue("工位");
row1.CreateCell().SetCellValue("备注");
row1.CreateCell().SetCellValue("扫描时间");
row1.CreateCell().SetCellValue("输入时间");
row1.CreateCell().SetCellValue("Eff");
row1.CreateCell().SetCellValue("Isc");
row1.CreateCell().SetCellValue("Voc");
row1.CreateCell().SetCellValue("Rs");
row1.CreateCell().SetCellValue("Rsh");
row1.CreateCell().SetCellValue("Vpm");
row1.CreateCell().SetCellValue("FF");
row1.CreateCell().SetCellValue("Sun");
row1.CreateCell().SetCellValue("Temp");
row1.CreateCell().SetCellValue("Class");
//将数据逐步写入sheet1各个行
string strSql = "where AkFqc.BarCode like '%@param%' and (AkFqc.DateTime between '@begin' and '@end') and AkFqc.LineTitle like '%@line%' and AkFqc.StationTitle like '%@station%'";
strSql = strSql.Replace("@param", searchString);
strSql = strSql.Replace("@begin", begin);
strSql = strSql.Replace("@end", end);
strSql = strSql.Replace("@line", line);
strSql = strSql.Replace("@station", station); List<AkFqc> pageResult = _akFqcRepository.GetPageList(, , strSql, "order by AkFqc.Id desc");
for (int i = ; i < pageResult.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + ); rowtemp.CreateCell().SetCellValue(pageResult[i].OrderNumber);
rowtemp.CreateCell().SetCellValue(pageResult[i].BarCode);
rowtemp.CreateCell().SetCellValue(pageResult[i].Title);
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Pmax)));
rowtemp.CreateCell().SetCellValue(pageResult[i].PTitle);
rowtemp.CreateCell().SetCellValue(pageResult[i].PScope);
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Ipm)));
rowtemp.CreateCell().SetCellValue(pageResult[i].ITitle);
rowtemp.CreateCell().SetCellValue(pageResult[i].IScope);
rowtemp.CreateCell().SetCellValue(pageResult[i].Spec);
rowtemp.CreateCell().SetCellValue(pageResult[i].ProductLevel);
rowtemp.CreateCell().SetCellValue(pageResult[i].BatteryLevel);
rowtemp.CreateCell().SetCellValue(pageResult[i].BadReason);
rowtemp.CreateCell().SetCellValue(pageResult[i].JudgeResult);
rowtemp.CreateCell().SetCellValue(pageResult[i].Employee);
rowtemp.CreateCell().SetCellValue(pageResult[i].LineTitle);
rowtemp.CreateCell().SetCellValue(pageResult[i].StationTitle);
rowtemp.CreateCell().SetCellValue(pageResult[i].Remark);
rowtemp.CreateCell().SetCellValue(pageResult[i].DateTime);
rowtemp.CreateCell().SetCellValue(pageResult[i].ScanDate);
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Eff)));
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Isc)));
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Voc)));
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Rs)));
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Rsh)));
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Vpm)));
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].FF)));
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Sun)));
rowtemp.CreateCell().SetCellValue(Convert.ToDouble(string.Format("{0:0.00000}", pageResult[i].Temp)));
rowtemp.CreateCell().SetCellValue(pageResult[i].Class); rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = textStyle;
rowtemp.GetCell().CellStyle = dateStyle;
rowtemp.GetCell().CellStyle = dateStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = numberStyle;
rowtemp.GetCell().CellStyle = textStyle;
}
//写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
workbook.Write(ms);
Response.BinaryWrite(ms.ToArray()); Response.Flush();
Response.End();
}