| | |
| | | using System.Collections; |
| | | using System.Data; |
| | | using System.Text; |
| | | using System.Timers; |
| | | using Masuit.Tools; |
| | | using NPOI.HSSF.UserModel; |
| | | using NPOI.HSSF.Util; |
| | | using NPOI.SS.Formula.Functions; |
| | |
| | | |
| | | //sheet.SetAutoFilter(new CellRangeAddress(0, 0, 0, dtSource.Columns.Count - 1)); //首行筛选 |
| | | // if (fixW <= 0) |
| | | sheet.CreateFreezePane(dtSource.Columns.Count, |
| | | 1 + aryHeader.Count); //首行冻结 |
| | | //sheet.CreateFreezePane(dtSource.Columns.Count, |
| | | // 1 + aryHeader.Count); //首行冻结 |
| | | //填充内容 |
| | | var dataRow = sheet.CreateRow(roct + 1); |
| | | for (var i = 0; i < dtSource.Rows.Count; i++) |
| | |
| | | //从第7行开始,就是循环数据库 |
| | | int _idx = 7; |
| | | DataTable tb1 = dtSource.Tables[1]; |
| | | string dd = ""; |
| | | bool _blPscs = false; |
| | | bool _blGjyzx = false; |
| | | foreach (DataRow rrr in tb1.Rows) |
| | | { |
| | | //增加品水测试标题 |
| | | if (_blPscs == false && rrr["项目"].ToString() == "品水测试") |
| | | { |
| | | IRow row700 = sheet.CreateRow(_idx); |
| | | for (int i = 0; i < ary.Length; i++) |
| | | { |
| | | ICell cell700 = row700.CreateCell(i); |
| | | cell700.CellStyle = style; |
| | | if (i == 0) |
| | | cell700.SetCellValue(rrr["项目"].ToString()); |
| | | if (i == 1) |
| | | cell700.SetCellValue("温度"); |
| | | if (i == 3) |
| | | cell700.SetCellValue("流量"); |
| | | if (i == 5) |
| | | cell700.SetCellValue("是否有异味"); |
| | | if (i == 6) |
| | | cell700.SetCellValue("检验结果"); |
| | | } |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 1, 2)); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 3, 4)); |
| | | _idx++; |
| | | _blPscs = true; |
| | | } |
| | | //增加标题 |
| | | if (_blGjyzx == false && rrr["项目"].ToString() == "关键部件一致性") |
| | | { |
| | | IRow row700 = sheet.CreateRow(_idx); |
| | | for (int i = 0; i < ary.Length; i++) |
| | | { |
| | | ICell cell700 = row700.CreateCell(i); |
| | | cell700.CellStyle = style; |
| | | if (i == 0) |
| | | cell700.SetCellValue(rrr["项目"].ToString()); |
| | | if (i == 1) |
| | | cell700.SetCellValue("关键名称"); |
| | | if (i == 2) |
| | | cell700.SetCellValue("生产日期"); |
| | | if (i == 3) |
| | | cell700.SetCellValue("使用数量"); |
| | | if (i == 4) |
| | | cell700.SetCellValue("认证信息"); |
| | | if (i == 5) |
| | | cell700.SetCellValue("是否符合CCC一致性"); |
| | | if (i == 6) |
| | | cell700.SetCellValue("操作"); |
| | | } |
| | | _idx++; |
| | | _blGjyzx = true; |
| | | } |
| | | |
| | | IRow row7 = sheet.CreateRow(_idx); |
| | | row7.HeightInPoints = rowHeight; |
| | | string _tmphb = rrr["项目合并行数"].ToString(); |
| | | string _tmp = ""; |
| | | if (dd == rrr["项目"].ToString()) |
| | | _tmp = ""; |
| | | else |
| | | _tmp = rrr["项目"].ToString(); |
| | | dd = rrr["项目"].ToString(); |
| | | for (int i = 0; i < ary.Length; i++) |
| | | { |
| | | ICell cell7 = row7.CreateCell(i); |
| | | cell7.CellStyle = style; |
| | | if (i == 0) |
| | | { |
| | | cell7.SetCellValue(_tmp); |
| | | if (!string.IsNullOrEmpty(_tmp) && int.Parse(_tmphb) > 1) |
| | | { |
| | | if (rrr["项目"].ToString() == "品水测试" || rrr["项目"].ToString() == "关键部件一致性" ) |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx-1, _idx + int.Parse(_tmphb)-1, 0, 0)); |
| | | else |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx + int.Parse(_tmphb) - 1, 0, 0)); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | switch (rrr["项目"].ToString()) |
| | | { |
| | | case "制热性能": |
| | | cell7.SetCellValue(rrr["制热性能table"].ToString()); |
| | | row7.HeightInPoints = rowHeight * 5; |
| | | if (i == ary.Length - 1) |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 1, 6)); |
| | | break; |
| | | case "品水测试": |
| | | //度数 |
| | | if (i == 1) |
| | | cell7.SetCellValue(rrr["技术/品质要求"].ToString()); |
| | | if (i == 3) |
| | | cell7.SetCellValue(rrr["品水测试流量"].ToString()); |
| | | if (i == 5) |
| | | cell7.SetCellValue(rrr["是否有异味"].ToString()); |
| | | if (i == 6) |
| | | cell7.SetCellValue(rrr["检验结果"].ToString()); |
| | | if (i == ary.Length - 1) |
| | | { |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 1, 2)); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 3, 4)); |
| | | } |
| | | break; |
| | | case "关键部件一致性": |
| | | if (i == 1) |
| | | cell7.SetCellValue(rrr["技术/品质要求"].ToString()); |
| | | if (i == 2) |
| | | cell7.SetCellValue(rrr["生产日期"].ToString()); |
| | | if (i == 3) |
| | | cell7.SetCellValue(rrr["使用数量"].ToString()); |
| | | if (i == 4) |
| | | cell7.SetCellValue(rrr["认证信息"].ToString()); |
| | | if (i == 5) |
| | | cell7.SetCellValue(rrr["是否符合CCC一致性"].ToString()); |
| | | //if (i == 6) |
| | | // cell7.SetCellValue(rrr["检验结果"].ToString()); |
| | | break; |
| | | default: |
| | | cell7.SetCellValue(rrr[ary[i]].ToString()); |
| | | |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | _idx++; |
| | | } |
| | |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 0, 6)); |
| | | setCellVal(sheet, _idx, 0, "首件填写,来料检验"); |
| | | //****合并好后开始赋值end************** |
| | | |
| | | // 5. 第8到10行中的第一列应被合并 |
| | | //sheet.AddMergedRegion(new CellRangeAddress(7, 9, 0, 0)); |
| | | |
| | | //// 6. 第17,18行中的第一列合并 |
| | | //sheet.AddMergedRegion(new CellRangeAddress(16, 17, 0, 0)); |
| | | |
| | | //// 7. 第17行中的2,3,4,5列合并 |
| | | //sheet.AddMergedRegion(new CellRangeAddress(16, 16, 1, 4)); |
| | | |
| | | //// 8. 第18行中的,2,3列合并,4,5,6列合并 |
| | | //sheet.AddMergedRegion(new CellRangeAddress(17, 17, 1, 2)); |
| | | //sheet.AddMergedRegion(new CellRangeAddress(17, 17, 3, 5)); |
| | | |
| | | //// 9. 第19行中的,列2,3,4合并 |
| | | //sheet.AddMergedRegion(new CellRangeAddress(18, 18, 1, 3)); |
| | | |
| | | //// 10. 第20行中的列全部合并 |
| | | //sheet.AddMergedRegion(new CellRangeAddress(19, 19, 0, 5)); |
| | | |
| | | //保存 |
| | | using (var ms = new MemoryStream()) |
| | | { |
| | |
| | | setCellVal(sheet, 6, 6, "不合格数"); |
| | | setCellVal(sheet, 6, 7, row0["bhgs"].ToString()); |
| | | setCellVal(sheet, 7, 0, "判定Ac/Re"); |
| | | setCellVal(sheet, 7, 1, "0/1"); |
| | | setCellVal(sheet, 7, 2, "0/1"); |
| | | setCellVal(sheet, 7, 3, "0/1"); |
| | | setCellVal(sheet, 7, 1, row0["cr"].ToString()); |
| | | setCellVal(sheet, 7, 2, row0["ma"].ToString()); |
| | | setCellVal(sheet, 7, 3, row0["mi"].ToString()); |
| | | setCellVal(sheet, 8, 0, "项目"); |
| | | setCellVal(sheet, 8, 1, "质量要求"); |
| | | setCellVal(sheet, 8, 6, "检验记录"); |
| | |
| | | //行高 |
| | | int rowHeight = 25; |
| | | //总列 |
| | | int maxCols = tb1.Columns.Count; |
| | | int maxCols = 10; |
| | | HSSFWorkbook workbook = new HSSFWorkbook(); |
| | | ISheet sheet = workbook.CreateSheet("Sheet1"); |
| | | |
| | | // 创建单元格样式 - 带边框和指定字体 |
| | | ICellStyle style = getStyle(workbook); |
| | | ICellStyle styleHeader = getStyle(workbook, 20, true, true); |
| | | ICellStyle style12 = getStyle(workbook, 11, false, true); |
| | | |
| | | // 设置列宽(6列) |
| | | // 设置列宽(10列) |
| | | sheet.SetColumnWidth(0, 3000); // 第一列稍宽 |
| | | sheet.SetColumnWidth(1, 3000); |
| | | sheet.SetColumnWidth(2, 3000); |
| | |
| | | sheet.SetColumnWidth(5, 3000); |
| | | sheet.SetColumnWidth(6, 3000); |
| | | sheet.SetColumnWidth(7, 3000); |
| | | // 创建行 |
| | | for (int rowIndex = 0; rowIndex <= 1; rowIndex++) |
| | | sheet.SetColumnWidth(8, 3000); |
| | | sheet.SetColumnWidth(9, 3000); |
| | | // 创建头行 |
| | | for (int rowIndex = 0; rowIndex <= 2; rowIndex++) |
| | | { |
| | | IRow row = sheet.CreateRow(rowIndex); |
| | | // 设置行高(所有行相同高度) |
| | |
| | | } |
| | | } |
| | | //第一行列合并 |
| | | sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 7)); |
| | | sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 9)); |
| | | setCellVal(sheet, 0, 0, row0["title"].ToString()); |
| | | //第二行列合并 |
| | | sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 1)); |
| | | sheet.AddMergedRegion(new CellRangeAddress(1, 1, 6, 9)); |
| | | setCellVal(sheet, 1, 0, "工单号"); |
| | | setCellVal(sheet, 1, 2, row0["daaNo"].ToString()); |
| | | setCellVal(sheet, 1, 3, "产品名称"); |
| | | setCellVal(sheet, 1, 4, row0["itemName"].ToString()); |
| | | setCellVal(sheet, 1, 5, "规格型号"); |
| | | setCellVal(sheet, 1, 6, row0["itemModel"].ToString()); |
| | | //第三行列合并 |
| | | sheet.AddMergedRegion(new CellRangeAddress(2, 2,2, 4)); |
| | | setCellVal(sheet,2, 0, "序号"); |
| | | setCellVal(sheet, 2, 1, "检验项目"); |
| | | setCellVal(sheet, 2, 2, "检验标准"); |
| | | setCellVal(sheet, 2, 5, row0["tm1"].ToString()); |
| | | setCellVal(sheet, 2, 6, row0["tm2"].ToString()); |
| | | setCellVal(sheet, 2, 7, row0["tm3"].ToString()); |
| | | setCellVal(sheet, 2, 8, row0["tm4"].ToString()); |
| | | setCellVal(sheet, 2, 9, row0["tm5"].ToString()); |
| | | //循环数据库 |
| | | int _idx = 1; |
| | | IRow row6 = sheet.CreateRow(_idx); |
| | | for (int colIndex = 0; colIndex < maxCols; colIndex++) |
| | | { |
| | | ICell cell6 = row6.CreateCell(colIndex); |
| | | cell6.CellStyle = style; |
| | | cell6.SetCellValue(tb1.Columns[colIndex].Caption.ToString().Trim()); |
| | | } |
| | | _idx++; |
| | | int _idx = 3; |
| | | foreach (DataRow rrr in tb1.Rows) |
| | | { |
| | | IRow row7 = sheet.CreateRow(_idx); |
| | | row7.HeightInPoints = rowHeight; |
| | | for (int colIndex = 0; colIndex < tb1.Columns.Count; colIndex++) |
| | | for (int colIndex = 0; colIndex < maxCols; colIndex++) |
| | | { |
| | | ICell cell7 = row7.CreateCell(colIndex); |
| | | cell7.CellStyle = style; |
| | | cell7.SetCellValue(rrr[tb1.Columns[colIndex]].ToString()); |
| | | // cell7.SetCellValue("1"); |
| | | switch (colIndex) |
| | | { |
| | | case 0: |
| | | cell7.SetCellValue(rrr["序号"].ToString()); |
| | | break; |
| | | case 1: |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 2, 4)); |
| | | cell7.SetCellValue(rrr["检验项目"].ToString()); |
| | | break; |
| | | case 2: |
| | | cell7.SetCellValue(rrr["检验标准"].ToString()); |
| | | break; |
| | | case 5: |
| | | cell7.SetCellValue(rrr["tm1"].ToString()); |
| | | break; |
| | | case 6: |
| | | cell7.SetCellValue(rrr["tm2"].ToString()); |
| | | break; |
| | | case 7: |
| | | cell7.SetCellValue(rrr["tm3"].ToString()); |
| | | break; |
| | | case 8: |
| | | cell7.SetCellValue(rrr["tm4"].ToString()); |
| | | break; |
| | | case 9: |
| | | cell7.SetCellValue(rrr["tm5"].ToString()); |
| | | break; |
| | | } |
| | | } |
| | | _idx++; |
| | | } |
| | | //备注 |
| | | ////备注 |
| | | IRow row8 = sheet.CreateRow(_idx); |
| | | row8.HeightInPoints = rowHeight; |
| | | for (int colIndex = 0; colIndex < maxCols; colIndex++) |
| | |
| | | ICell cell = row8.CreateCell(colIndex); |
| | | cell.CellStyle = style12; |
| | | } |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 0, 2)); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 0, 4)); |
| | | setCellVal(sheet, _idx, 0, "巡检人:"); |
| | | for (int colIndex = 2; colIndex < tb1.Columns.Count; colIndex++) |
| | | { |
| | | setCellVal(sheet, _idx, colIndex, "余吉林"); |
| | | } |
| | | setCellVal(sheet, _idx, 5, row0["xjr1"].ToString()); |
| | | setCellVal(sheet, _idx, 6, row0["xjr2"].ToString()); |
| | | setCellVal(sheet, _idx, 7, row0["xjr3"].ToString()); |
| | | setCellVal(sheet, _idx, 8, row0["xjr4"].ToString()); |
| | | setCellVal(sheet, _idx, 9, row0["xjr5"].ToString()); |
| | | //保存 |
| | | using (var ms = new MemoryStream()) |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 生成成品检验报告(按实际模板结构) |
| | | /// </summary> |
| | | /// <param name="dtSource"></param> |
| | | /// <param name="strFileName"></param> |
| | | public static void ExportChengPin(DataSet dtSource, string strFileName) |
| | | { |
| | | //行高 |
| | | int rowHeight = 25; |
| | | //总列数 - 根据模板分析,至少需要18列 |
| | | int maxCols = 18; |
| | | // 创建工作簿 |
| | | HSSFWorkbook workbook = new HSSFWorkbook(); |
| | | ISheet sheet = workbook.CreateSheet("Sheet1"); |
| | | |
| | | // 创建单元格样式 |
| | | ICellStyle style = getStyle(workbook); |
| | | ICellStyle styleHeader = getStyle(workbook, 16, true, true); |
| | | ICellStyle styleTitle = getStyle(workbook, 14, true, true); |
| | | ICellStyle styleSmall = getStyle(workbook, 10, false, false); |
| | | |
| | | // 设置列宽 |
| | | sheet.SetColumnWidth(0, 3500); // A列:项目名称 |
| | | sheet.SetColumnWidth(1, 5000); // B列:标准/规范 |
| | | for (int i = 2; i < 18; i++) // C到R列:16个检测值列 |
| | | { |
| | | sheet.SetColumnWidth(i, 2500); |
| | | } |
| | | |
| | | //获取表头数据 |
| | | DataTable tb0 = dtSource.Tables[0]; |
| | | DataRow row0 = tb0.Rows[0]; |
| | | int _idx = 0; |
| | | |
| | | // 第1行:报告标题 |
| | | IRow titleRow = sheet.CreateRow(_idx); |
| | | titleRow.HeightInPoints = 30; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = titleRow.CreateCell(i); |
| | | cell.CellStyle = styleHeader; |
| | | } |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 0, maxCols - 1)); |
| | | setCellVal(sheet, _idx, 0, "成品检验报告"); |
| | | _idx++; |
| | | |
| | | // 第2行:基本信息第一行 |
| | | IRow info1Row = sheet.CreateRow(_idx); |
| | | info1Row.HeightInPoints = rowHeight; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = info1Row.CreateCell(i); |
| | | cell.CellStyle = style; |
| | | } |
| | | setCellVal(sheet, _idx, 0, "生产车间:" + (row0["workShop"]?.ToString() ?? "")); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 0, 5)); |
| | | |
| | | setCellVal(sheet, _idx, 12, "室温:" + (row0["temperature"]?.ToString() ?? "")); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 12, 17)); |
| | | _idx++; |
| | | |
| | | // 第3行:产品信息 |
| | | IRow info2Row = sheet.CreateRow(_idx); |
| | | info2Row.HeightInPoints = rowHeight; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = info2Row.CreateCell(i); |
| | | cell.CellStyle = style; |
| | | } |
| | | setCellVal(sheet, _idx, 0, "产品名称"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 1, 4)); |
| | | setCellVal(sheet, _idx, 1, row0["itemName"]?.ToString() ?? ""); |
| | | |
| | | setCellVal(sheet, _idx, 5, "型号规格"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 6, 11)); |
| | | setCellVal(sheet, _idx, 6, row0["itemModel"]?.ToString() ?? ""); |
| | | |
| | | setCellVal(sheet, _idx, 12, "电压"); |
| | | setCellVal(sheet, _idx, 13, row0["voltage"]?.ToString() ?? ""); |
| | | |
| | | setCellVal(sheet, _idx, 14, "线号"); |
| | | setCellVal(sheet, _idx, 15, row0["lineNo"]?.ToString() ?? ""); |
| | | _idx++; |
| | | |
| | | // 第4行:商标等信息 |
| | | IRow info3Row = sheet.CreateRow(_idx); |
| | | info3Row.HeightInPoints = rowHeight; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = info3Row.CreateCell(i); |
| | | cell.CellStyle = style; |
| | | } |
| | | setCellVal(sheet, _idx, 0, "商标"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 1, 4)); |
| | | setCellVal(sheet, _idx, 1, row0["brand"]?.ToString() ?? ""); |
| | | |
| | | setCellVal(sheet, _idx, 5, "生产日期"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 6, 11)); |
| | | setCellVal(sheet, _idx, 6, row0["productionDate"]?.ToString() ?? ""); |
| | | |
| | | setCellVal(sheet, _idx, 12, "班次"); |
| | | setCellVal(sheet, _idx, 13, row0["classes"]?.ToString() ?? ""); |
| | | |
| | | setCellVal(sheet, _idx, 14, "批量"); |
| | | setCellVal(sheet, _idx, 15, row0["batch"]?.ToString() ?? ""); |
| | | _idx++; |
| | | |
| | | // 第5行:AC/Re标准 |
| | | IRow acreRow = sheet.CreateRow(_idx); |
| | | acreRow.HeightInPoints = rowHeight; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = acreRow.CreateCell(i); |
| | | cell.CellStyle = style; |
| | | } |
| | | setCellVal(sheet, _idx, 0, "Ac/Re(A类)"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 1, 5)); |
| | | setCellVal(sheet, _idx, 1, row0["acRe_A"]?.ToString() ?? ""); |
| | | |
| | | setCellVal(sheet, _idx, 6, "Ac/Re(B类)"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 7, 11)); |
| | | setCellVal(sheet, _idx, 7, row0["acRe_B"]?.ToString() ?? ""); |
| | | |
| | | setCellVal(sheet, _idx, 12, "Ac/Re(C类)"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 13, 17)); |
| | | setCellVal(sheet, _idx, 13, row0["acRe_C"]?.ToString() ?? ""); |
| | | _idx++; |
| | | |
| | | // 第6行:抽样信息 |
| | | IRow sampleRow = sheet.CreateRow(_idx); |
| | | sampleRow.HeightInPoints = rowHeight; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = sampleRow.CreateCell(i); |
| | | cell.CellStyle = style; |
| | | } |
| | | setCellVal(sheet, _idx, 0, "抽样"); |
| | | setCellVal(sheet, _idx, 1, "匀速"); |
| | | setCellVal(sheet, _idx, 2, row0["SampleMethod"]?.ToString() == "匀速" ? "√" : ""); |
| | | setCellVal(sheet, _idx, 3, "随机"); |
| | | setCellVal(sheet, _idx, 4, row0["SampleMethod"]?.ToString() == "随机" ? "√" : ""); |
| | | |
| | | setCellVal(sheet, _idx, 5, "样本数"); |
| | | setCellVal(sheet, _idx, 6, "匀速抽样数"); |
| | | setCellVal(sheet, _idx, 7, row0["sampleSize1"]?.ToString() ?? ""); |
| | | setCellVal(sheet, _idx, 8, "随机抽样数"); |
| | | setCellVal(sheet, _idx, 9, row0["sampleSize2"]?.ToString() ?? ""); |
| | | setCellVal(sheet, _idx, 10, "样本"); |
| | | setCellVal(sheet, _idx, 11, row0["sampleSize3"]?.ToString() ?? ""); |
| | | _idx++; |
| | | |
| | | // 第7行:检验项目表头 |
| | | IRow headerRow = sheet.CreateRow(_idx); |
| | | headerRow.HeightInPoints = rowHeight; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = headerRow.CreateCell(i); |
| | | cell.CellStyle = styleTitle; |
| | | } |
| | | setCellVal(sheet, _idx, 0, "项目"); |
| | | setCellVal(sheet, _idx, 1, "标准"); |
| | | setCellVal(sheet, _idx, 2, "1"); |
| | | setCellVal(sheet, _idx, 3, "2"); |
| | | // 根据需要继续添加更多列标题,这里简化为主要列 |
| | | for (int i = 4; i < 18; i++) |
| | | { |
| | | setCellVal(sheet, _idx, i, (i-1).ToString()); |
| | | } |
| | | _idx++; |
| | | |
| | | // 检验项目数据 |
| | | if (dtSource.Tables.Count > 1) |
| | | { |
| | | DataTable tb1 = dtSource.Tables[1]; |
| | | foreach (DataRow rrr in tb1.Rows) |
| | | { |
| | | IRow dataRow = sheet.CreateRow(_idx); |
| | | dataRow.HeightInPoints = rowHeight; |
| | | |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = dataRow.CreateCell(i); |
| | | cell.CellStyle = style; |
| | | } |
| | | |
| | | setCellVal(sheet, _idx, 0, rrr["RPB003"]?.ToString() ?? ""); |
| | | setCellVal(sheet, _idx, 1, rrr["RPB004"]?.ToString() ?? ""); |
| | | |
| | | // 填充16个测试值 |
| | | for (int i = 0; i < 16; i++) |
| | | { |
| | | string colName = $"RPB{i + 5:D3}"; // RPB005, RPB006, ..., RPB020 |
| | | if (rrr.Table.Columns.Contains(colName)) |
| | | { |
| | | setCellVal(sheet, _idx, i + 2, rrr[colName]?.ToString() ?? ""); |
| | | } |
| | | else |
| | | { |
| | | setCellVal(sheet, _idx, i + 2, ""); |
| | | } |
| | | } |
| | | |
| | | _idx++; |
| | | } |
| | | } |
| | | |
| | | // 不合格内容记录 |
| | | IRow defectHeaderRow = sheet.CreateRow(_idx); |
| | | defectHeaderRow.HeightInPoints = rowHeight; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = defectHeaderRow.CreateCell(i); |
| | | cell.CellStyle = styleTitle; |
| | | } |
| | | setCellVal(sheet, _idx, 0, "序号"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 1, 10)); // 不合格内容占多列 |
| | | setCellVal(sheet, _idx, 1, "不合格内容"); |
| | | setCellVal(sheet, _idx, 11, "A类"); |
| | | setCellVal(sheet, _idx, 12, "B类"); |
| | | setCellVal(sheet, _idx, 13, "C类"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 14, 17)); // 注意点占剩余列 |
| | | setCellVal(sheet, _idx, 14, "注意点"); |
| | | _idx++; |
| | | |
| | | if (dtSource.Tables.Count > 2) |
| | | { |
| | | DataTable tb2 = dtSource.Tables[2]; |
| | | foreach (DataRow rrr in tb2.Rows) |
| | | { |
| | | if (rrr["Description"]?.ToString() == "不良数量") |
| | | break; |
| | | IRow dataRow = sheet.CreateRow(_idx); |
| | | dataRow.HeightInPoints = rowHeight; |
| | | |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = dataRow.CreateCell(i); |
| | | cell.CellStyle = style; |
| | | } |
| | | |
| | | setCellVal(sheet, _idx, 0, rrr["Seq"]?.ToString() ?? ""); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 1, 10)); // 不合格内容占多列 |
| | | setCellVal(sheet, _idx, 1, rrr["Description"]?.ToString() ?? ""); |
| | | setCellVal(sheet, _idx, 11, rrr["AClass"]?.ToString() ?? ""); |
| | | setCellVal(sheet, _idx, 12, rrr["BClass"]?.ToString() ?? ""); |
| | | setCellVal(sheet, _idx, 13, rrr["CClass"]?.ToString() ?? ""); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 14, 17)); // 注意点占剩余列 |
| | | setCellVal(sheet, _idx, 14, rrr["Point"]?.ToString() ?? ""); |
| | | _idx++; |
| | | } |
| | | |
| | | DataRow dr = tb2.Rows[tb2.Rows.Count - 1]; |
| | | // 备注行 |
| | | IRow remarkRow = sheet.CreateRow(_idx); |
| | | remarkRow.HeightInPoints = rowHeight; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = remarkRow.CreateCell(i); |
| | | cell.CellStyle = style; |
| | | } |
| | | setCellVal(sheet, _idx, 0, "备注:"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 1, 8)); |
| | | setCellVal(sheet, _idx, 9, "不良数量:"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 9, 10)); |
| | | setCellVal(sheet, _idx, 11, dr["AClass"]?.ToString() ?? ""); |
| | | setCellVal(sheet, _idx, 12, dr["BClass"]?.ToString() ?? ""); |
| | | setCellVal(sheet, _idx, 13, dr["CClass"]?.ToString() ?? ""); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 14, 17)); // 注意点占剩余列 |
| | | setCellVal(sheet, _idx, 14, dr["Point"]?.ToString() ?? ""); |
| | | _idx++; |
| | | |
| | | // 大的备注内容区域 |
| | | IRow remarkContentRow = sheet.CreateRow(_idx); |
| | | remarkContentRow.HeightInPoints = 60; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = remarkContentRow.CreateCell(i); |
| | | cell.CellStyle = style; |
| | | } |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 0, 17)); |
| | | setCellVal(sheet, _idx, 0, dr["Remark"]?.ToString() ?? ""); |
| | | _idx++; |
| | | } |
| | | |
| | | // 检验判定区域(3行布局) |
| | | for (int judgeRowIndex = 0; judgeRowIndex < 3; judgeRowIndex++) |
| | | { |
| | | IRow judgmentRow = sheet.CreateRow(_idx); |
| | | judgmentRow.HeightInPoints = rowHeight; |
| | | for (int i = 0; i < maxCols; i++) |
| | | { |
| | | ICell cell = judgmentRow.CreateCell(i); |
| | | cell.CellStyle = styleTitle; |
| | | } |
| | | |
| | | if (judgeRowIndex == 0) |
| | | { |
| | | // 第一行:检验判定 |
| | | setCellVal(sheet, _idx, 0, "检验判定"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx + 2, 0, 0)); // 检验判定跨3行 |
| | | |
| | | if (row0["CheckResult"]?.ToString() == "合格") |
| | | { |
| | | setCellVal(sheet, _idx, 1, "√ 合格 □ 不合格"); |
| | | } |
| | | else if(row0["CheckResult"]?.ToString() == "不合格") |
| | | { |
| | | setCellVal(sheet, _idx, 1, "□ 合格 √ 不合格"); |
| | | } |
| | | else |
| | | { |
| | | setCellVal(sheet, _idx, 1, "□ 合格 □ 不合格"); |
| | | } |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx + 1, 1, 5)); // 合格/不合格跨2行 |
| | | |
| | | setCellVal(sheet, _idx, 6, "审核"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx + 2, 6, 6)); // 审核跨3行 |
| | | |
| | | if (row0["ProcessResults"]?.ToString() == "无") |
| | | { |
| | | setCellVal(sheet, _idx, 7, "√ 无 □ 返工"); |
| | | } |
| | | else if (row0["ProcessResults"]?.ToString() == "返工") |
| | | { |
| | | setCellVal(sheet, _idx, 7, "□ 无 √ 返工"); |
| | | } |
| | | else |
| | | { |
| | | setCellVal(sheet, _idx, 7, "□ 无 □ 返工"); |
| | | } |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 7, 11)); |
| | | |
| | | setCellVal(sheet, _idx, 12, "审批"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx + 2, 12, 12)); // 审批跨3行 |
| | | |
| | | if (row0["ProcessResults"]?.ToString() == "无") |
| | | { |
| | | setCellVal(sheet, _idx, 13, "√ 无 □ 返工"); |
| | | } |
| | | else if (row0["ProcessResults"]?.ToString() == "返工") |
| | | { |
| | | setCellVal(sheet, _idx, 13, "□ 无 √ 返工"); |
| | | } |
| | | else |
| | | { |
| | | setCellVal(sheet, _idx, 13, "□ 无 □ 返工"); |
| | | } |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 13, 17)); |
| | | } |
| | | else if (judgeRowIndex == 1) |
| | | { |
| | | if (row0["ProcessResults"]?.ToString() == "让步接收") |
| | | { |
| | | setCellVal(sheet, _idx, 7, "√ 让步接收 □ 特采"); |
| | | } |
| | | else if (row0["ProcessResults"]?.ToString() == "特采") |
| | | { |
| | | setCellVal(sheet, _idx, 7, "□ 让步接收 √ 特采"); |
| | | } |
| | | else |
| | | { |
| | | setCellVal(sheet, _idx, 7, "□ 让步接收 □ 特采"); |
| | | } |
| | | // 第二行:让步接收选项 |
| | | |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 7, 11)); |
| | | |
| | | if (row0["ProcessResults"]?.ToString() == "让步接收") |
| | | { |
| | | setCellVal(sheet, _idx, 13, "√ 让步接收 □ 特采"); |
| | | } |
| | | else if (row0["ProcessResults"]?.ToString() == "特采") |
| | | { |
| | | setCellVal(sheet, _idx, 13, "□ 让步接收 √ 特采"); |
| | | } |
| | | else |
| | | { |
| | | setCellVal(sheet, _idx, 13, "□ 让步接收 □ 特采"); |
| | | } |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 13, 17)); |
| | | } |
| | | else |
| | | { |
| | | // 签名行 |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 1, 2)); |
| | | setCellVal(sheet, _idx, 1, $"签名/日期:"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 3, 5)); |
| | | setCellVal(sheet, _idx, 3, $"{row0["JY_USER"]?.ToString() ?? ""}/{row0["JY_DATE"]?.ToString() ?? ""}"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 7, 8)); |
| | | setCellVal(sheet, _idx, 7, $"签名/日期:"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 9, 11)); |
| | | setCellVal(sheet, _idx, 9, $"{row0["CHECK_USER"]?.ToString() ?? ""}/{row0["CHECK_DATE"]?.ToString() ?? ""}"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 13, 14)); |
| | | setCellVal(sheet, _idx, 13, $"签名/日期:"); |
| | | sheet.AddMergedRegion(new CellRangeAddress(_idx, _idx, 15, 17)); |
| | | setCellVal(sheet, _idx, 15, $"{row0["SP_USER"]?.ToString() ?? ""}/{row0["SP_DATE"]?.ToString() ?? ""}"); |
| | | } |
| | | _idx++; |
| | | } |
| | | //保存 |
| | | using (var ms = new MemoryStream()) |
| | | { |
| | | using (var fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write)) |
| | | { |
| | | workbook.Write(ms); |
| | | ms.Flush(); |
| | | ms.Position = 0; |
| | | var data = ms.ToArray(); |
| | | fs.Write(data, 0, data.Length); |
| | | fs.Flush(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// Excel导入成Datable |