public void Export(DataRequest<ExportModel> request, DataResponse<dynamic> response) { try { var order = GetShopModel(request.ObjectData.guid); var newFile = Directory.GetCurrentDirectory() + "\\Export\\Order" + order.OrderDate + ".xls"; string p = System.IO.Path.GetDirectoryName(newFile); if (!System.IO.Directory.Exists(p)) System.IO.Directory.CreateDirectory(p); var ShopGid = order.ShopGid; string startTime = order.OrderDate.Split('~')[0] + " 00:00:00"; string endTime = order.OrderDate.Split('~')[1] + " 23:59:59"; using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write)) { IWorkbook workbook = new XSSFWorkbook(); #region 車輛銷售訂單明細 ISheet sheet1 = workbook.CreateSheet("門店銷售訂單明細"); //index表明多少行
var rowIndex = 0; //建立表頭行
IRow row = sheet1.CreateRow(rowIndex); row.HeightInPoints = 30;//行高
row.CreateCell(0).SetCellValue("門店商鋪"); row.CreateCell(1).SetCellValue("商鋪號碼"); row.CreateCell(2).SetCellValue("銷售單號"); row.CreateCell(3).SetCellValue("提車時間"); //填充數據
List<ExportOrderModel> OrderShops = GetModelExl(ShopGid.Value, startTime, endTime); for (int i = 0; i < OrderShops.Count; i++) { IRow row1 = sheet1.CreateRow(i + 1); ICell cell = row1.CreateCell(0); //建立第一列
#region 標題行設置字體 ICellStyle style = workbook.CreateCellStyle();//建立樣式對象 //設置單元格的樣式:水平對齊填充
style.Alignment = HorizontalAlignment.Left; style.VerticalAlignment = VerticalAlignment.Center;//垂直居中 //自動換行
style.WrapText = true; IFont font = workbook.CreateFont(); //建立一個字體樣式對象
font.FontName = "宋體"; //和excel裏面的字體對應
font.IsItalic = false; //斜體
font.FontHeightInPoints =15 ;//字體大小 //font.Boldweight = short.MaxValue;//字體加粗
font.Boldweight = (Int16)FontBoldWeight.Bold;//加粗;
style.SetFont(font); //將字體樣式賦給樣式對象 //cell.CellStyle = style; //把樣式賦給單元格
/*修改指定單元格樣式 若是要修改行樣式則須要將row.Cells.Count循環出來,挨個設置!*/
// row.Cells[1].CellStyle = style;
for (int k = 0; k < row.Cells.Count; k++) { row.Cells[k].CellStyle = style; //列寬25 //設置列寬
sheet1.SetColumnWidth(k, 25 * 256); } #endregion
#region 單元格列值屬性樣式
//for (int jj = 0; jj < row1.Cells.Count; jj++) //{ // row1.Cells[jj].CellStyle = style; //} //cell.CellStyle = style;
#endregion cell.SetCellValue(OrderShops[i].ShopName); cell = row1.CreateCell(1);//設置單元格的值 //設置列值樣式 //row1.CreateCell(0).CellStyle = style;
cell.SetCellValue(order.ShopNo); cell = row1.CreateCell(2); cell.SetCellValue(OrderShops[i].OrderSaleNo); cell = row1.CreateCell(3); cell.SetCellValue(OrderShops[i].PayDateTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); } #endregion
#endregion
#region 車輛銷售換貨明細表格
//建立表頭
ISheet sheet6 = workbook.CreateSheet("車輛銷售換貨明細表格"); IRow row6 = sheet6.CreateRow(0); row6.HeightInPoints = 30;//行高
row6.CreateCell(0).SetCellValue("換貨單號"); row6.CreateCell(1).SetCellValue("新車輛顏色"); row6.CreateCell(2).SetCellValue("原車輛型號類型"); var table = GetChangeOrder(ShopGid.Value, startTime, endTime); j = 0; foreach (DataRow item in table.Rows) { IRow row1 = sheet6.CreateRow(j + 1); ICell cell = row1.CreateCell(0); #region 標題行設置字體 ICellStyle style = workbook.CreateCellStyle();//建立樣式對象 //設置單元格的樣式:水平對齊填充
style.Alignment = HorizontalAlignment.Left; style.VerticalAlignment = VerticalAlignment.Center;//垂直居中 //自動換行
style.WrapText = true; IFont font = workbook.CreateFont(); //建立一個字體樣式對象
font.FontName = "宋體"; //和excel裏面的字體對應
font.IsItalic = false; //斜體
font.FontHeightInPoints = 15;//字體大小 //font.Boldweight = short.MaxValue;//字體加粗
font.Boldweight = (Int16)FontBoldWeight.Bold;//加粗;
style.SetFont(font); //將字體樣式賦給樣式對象 //cell.CellStyle = style; //把樣式賦給單元格
/*修改指定單元格樣式 若是要修改行樣式則須要將row.Cells.Count循環出來,挨個設置!*/
// row.Cells[1].CellStyle = style;
for (int k = 0; k < row6.Cells.Count; k++) { row6.Cells[k].CellStyle = style; //列寬25
sheet6.SetColumnWidth(k, 25 * 256); } #endregion cell.SetCellValue(DataConvertHelper.GetString(item["ChangeNo"])); cell = row1.CreateCell(1); cell.SetCellValue(DataConvertHelper.GetString(item["OrderColor"])); cell = row1.CreateCell(2); cell.SetCellValue(DataConvertHelper.GetString(item["OrderType"])); j++; } #endregion workbook.Write(fs); } response.Tag = Common.ExportFile.ExportFlag; response.ObjectData = new Common.ExportFile { FilePath = newFile, FileName = "訂單信息" + 20190101 + ".xls" }; } catch (Exception ex) { LogHelper.Debug("異常" + ex.Message, "導出異常", this.GetType().ToString()); response.ObjectData= ex.Message; } }