using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text.RegularExpressions; using System.Web.Http; using Warehouse.DAL.Report; using Warehouse.WebApi.Model; namespace Warehouse.WebApi { public class StorageNewController : ApiController { StorageReportDAL DT = new StorageReportDAL(); [Route("api/StorageReportNew/StorageReportsNew")] [HttpPost] public IHttpActionResult StorageReport(StorageModel model) { //string bt, string et, string name, string locationname, string containerno, string palletno, string final_grade, string power_grade string bt = model.TestTimeBegin; string et = model.TestTimeEnd; string name =model.Stock; string locationname = model.Location; string final_grade = model.FinalGrade; string power_grade = model.PowerGrade; string containerno_old = model.Containerno; string trim = Regex.Replace(containerno_old, @"\s", " "); string containerno = "'" + trim.Replace(" ", "','") + "'"; string palletno_old = model.Palletno; string trim1 = Regex.Replace(palletno_old, @"\s", " "); string palletno = "'" + trim1.Replace(" ", "','") + "'"; string workorder = model.WorkOrder; string saleorder = model.SaleOrder; string area_code = model.WorkShop; string serial = model.SerialNum; var res = DT.Storage(bt, et, name, locationname, containerno, palletno, final_grade, power_grade,workorder,saleorder,area_code,serial); return Json(res); } #region 下载 [Route("api/StorageReportNew/StorageReportsNewGet")] [HttpPost] public HttpResponseMessage StorageReportsNewGet(StorageModel model) { try { HSSFWorkbook hssfworkbook = new HSSFWorkbook(); hssfworkbook.CreateSheet("入库报表"); ICellStyle style = hssfworkbook.CreateCellStyle(); //设置单元格的样式:水平对齐居中 style.Alignment = HorizontalAlignment.Center; ISheet sheet = null; sheet = hssfworkbook.GetSheetAt(0); IRow row = null; #region 添加表头 var thList = new List { "入库时间", "仓库", "库位", "订单号", "工单号", "柜号", "托盘号", "序列号", "开路电压Voc(V)", "短路电流Isc(A)", "工作电压Vpm(V)", "工作电流Ipm(A)", "实测功率Pmax(W)", "填充因子FF", "电流档current(A)", "产品类型", "功率档", "栅数", "等级", "组件尺寸", "接线盒类型", "状态", "车间" }; //设置第一列列宽 // sheet.SetColumnWidth(0, 20 * 256); //设置第一列之后的列宽 for (int i = 1; i < thList.Count; i++) { sheet.SetColumnWidth(i, 10 * 500); } var rowCount = sheet.LastRowNum; row = sheet.CreateRow(rowCount); //添加表头并设置样式字体居中 for (int i = 0; i < thList.Count; i++) { row.CreateCell(i).SetCellValue(thList[i]); row.GetCell(i).CellStyle = style; } #endregion //获取数据 string bt = model.TestTimeBegin; string et = model.TestTimeEnd; string name = model.Stock; string locationname = model.Location; string final_grade = model.FinalGrade; string power_grade = model.PowerGrade; string containerno_old = model.Containerno; string trim = Regex.Replace(containerno_old, @"\s", " "); string containerno = "'" + trim.Replace(" ", "','") + "'"; string palletno_old = model.Palletno; string trim1 = Regex.Replace(palletno_old, @"\s", " "); string palletno = "'" + trim1.Replace(" ", "','") + "'"; string workorder = model.WorkOrder; string saleorder = model.SaleOrder; string area_code = model.WorkShop; string serial = model.SerialNum; var res = DT.Storage(bt, et, name, locationname, containerno, palletno, final_grade, power_grade, workorder, saleorder, area_code, serial); foreach (var item in res.ToList()) { var rowCount1 = sheet.LastRowNum + 1; row = sheet.CreateRow(rowCount1); row.CreateCell(0).SetCellValue(item.createtime.ToString()); row.GetCell(0).CellStyle = style; row.CreateCell(1).SetCellValue(item.name.ToString()); row.GetCell(1).CellStyle = style; row.CreateCell(2).SetCellValue(item.locationname.ToString()); row.GetCell(2).CellStyle = style; row.CreateCell(3).SetCellValue(item.sale_order.ToString()); row.GetCell(3).CellStyle = style; row.CreateCell(4).SetCellValue(item.workorder.ToString()); row.GetCell(4).CellStyle = style; row.CreateCell(5).SetCellValue(item.containerno.ToString()); row.GetCell(5).CellStyle = style; row.CreateCell(6).SetCellValue(item.palletno.ToString()); row.GetCell(6).CellStyle = style; row.CreateCell(7).SetCellValue(item.serial_nbr.ToString()); row.GetCell(7).CellStyle = style; row.CreateCell(8).SetCellValue(item.voc.ToString()); row.GetCell(8).CellStyle = style; row.CreateCell(9).SetCellValue(item.isc.ToString()); row.GetCell(9).CellStyle = style; row.CreateCell(10).SetCellValue(item.vpm.ToString()); row.GetCell(10).CellStyle = style; row.CreateCell(11).SetCellValue(item.ipm.ToString()); row.GetCell(11).CellStyle = style; row.CreateCell(12).SetCellValue(item.pmax.ToString()); row.GetCell(12).CellStyle = style; row.CreateCell(13).SetCellValue(item.ff.ToString()); row.GetCell(13).CellStyle = style; row.CreateCell(14).SetCellValue(item.current_grade.ToString()); row.GetCell(14).CellStyle = style; row.CreateCell(15).SetCellValue(item.product_code.ToString()); row.GetCell(15).CellStyle = style; row.CreateCell(16).SetCellValue(item.power_grade.ToString()); row.GetCell(16).CellStyle = style; row.CreateCell(17).SetCellValue(item.busbar_type_desc.ToString()); row.GetCell(17).CellStyle = style; row.CreateCell(18).SetCellValue(item.final_grade.ToString()); row.GetCell(18).CellStyle = style; row.CreateCell(19).SetCellValue(item.frame_type_desc.ToString()); row.GetCell(19).CellStyle = style; row.CreateCell(20).SetCellValue(item.jbox_type_desc.ToString()); row.GetCell(20).CellStyle = style; row.CreateCell(21).SetCellValue(item.statuss.ToString()); row.GetCell(21).CellStyle = style; row.CreateCell(22).SetCellValue(item.descriptions.ToString()); row.GetCell(22).CellStyle = style; } MemoryStream ms = new MemoryStream(); //创建内存流用于写入文件 hssfworkbook.Write(ms);//将Excel写入流 ms.Flush(); ms.Position = 0; HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(ms); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "入库报表.xls" }; return result; } catch (Exception) { } return new HttpResponseMessage(HttpStatusCode.NoContent); } #endregion } }