123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110 |
- using System.IO;
- using System.Data;
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.HSSF.Util;
- using NPOI.XSSF.UserModel;
- using System;
- using UAS_LabelMachine.Entity;
- using UAS_LabelMachine.PublicMethod;
- namespace UAS_LabelMachine
- {
- class ExcelHandler
- {
- DataHelper dh = SystemInf.dh;
-
-
-
- public string ExportExcel(DataTable firstsdt, DataTable dt, string FolderPath, string FileName, string Type, int PageSize, bool PrintWID)
- {
-
- MemoryStream ms;
- ms = DataTableToExcel1(firstsdt, dt, Type, PageSize, PrintWID);
-
- string filePath = @FolderPath + "\\" + FileName + ".xls";
- FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
- byte[] data = ms.ToArray();
- fs.Write(data, 0, data.Length);
- fs.Flush();
-
- ms.Dispose();
- fs.Dispose();
- return filePath;
- }
-
-
-
- public void ImportExcel(DataTable DataTable, string TableName)
- {
- int columnNum = DataTable.Columns.Count;
- int rowNum = DataTable.Columns.Count;
- string[] field = new string[columnNum];
- for (int i = 0; i < columnNum; i++)
- {
- field[i] = DataTable.Rows[0][i].ToString();
- }
- }
- public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
- {
- DataTable dataTable = null;
- FileStream fs = null;
- DataColumn column = null;
- DataRow dataRow = null;
- IWorkbook workbook = null;
- ISheet sheet = null;
- IRow row = null;
- ICell cell = null;
- int startRow = 0;
- try
- {
- using (fs = File.OpenRead(filePath))
- {
-
- if (filePath.IndexOf(".xlsx") > 0)
- {
- workbook = new XSSFWorkbook(fs);
- }
-
- else if (filePath.IndexOf(".xls") > 0)
- {
- workbook = new HSSFWorkbook(fs);
- }
- if (workbook != null)
- {
- sheet = workbook.GetSheetAt(0);
- dataTable = new DataTable();
- if (sheet != null)
- {
- int rowCount = sheet.LastRowNum;
- if (rowCount > 0)
- {
- IRow firstRow = sheet.GetRow(0);
- int cellCount = firstRow.LastCellNum;
-
- if (isColumnName)
- {
- startRow = 1;
- for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
- {
- cell = firstRow.GetCell(i);
- if (cell != null)
- {
- if (cell.StringCellValue != null)
- {
- column = new DataColumn(cell.StringCellValue);
- dataTable.Columns.Add(column);
- }
- }
- }
- }
- else
- {
- for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
- {
- column = new DataColumn("column" + (i + 1));
- dataTable.Columns.Add(column);
- }
- }
-
- for (int i = startRow; i <= rowCount; ++i)
- {
- row = sheet.GetRow(i);
- if (row == null) continue;
- dataRow = dataTable.NewRow();
- for (int j = row.FirstCellNum; j < cellCount; ++j)
- {
- cell = row.GetCell(j);
- if (cell == null)
- {
- dataRow[j] = "";
- }
- else
- {
-
- switch (cell.CellType)
- {
- case CellType.Blank:
- dataRow[j] = "";
- break;
- case CellType.Numeric:
- short format = cell.CellStyle.DataFormat;
-
- if (format == 14 || format == 31 || format == 57 || format == 58)
- dataRow[j] = cell.DateCellValue;
- else
- dataRow[j] = cell.NumericCellValue;
- break;
- case CellType.String:
- dataRow[j] = cell.StringCellValue;
- break;
- }
- }
- }
- dataTable.Rows.Add(dataRow);
- }
- }
- }
- }
- }
- return dataTable;
- }
- catch (Exception)
- {
- if (fs != null)
- {
- fs.Close();
- }
- return null;
- }
- }
-
-
-
-
-
- public MemoryStream DataTableToExcel(DataTable DataTable)
- {
-
- MemoryStream ms = new MemoryStream();
-
- HSSFWorkbook book = new HSSFWorkbook();
-
- ISheet sheet = book.CreateSheet("sheet1");
-
- int rowNum = DataTable.Rows.Count;
- int columnNum = DataTable.Columns.Count;
-
- for (int i = 0; i < columnNum; i++)
- {
- int dataLength;
-
- if (DataTable.Rows[0][i].ToString().Length < DataTable.Columns[i].ColumnName.Length)
- {
- dataLength = DataTable.Columns[i].ColumnName.Length;
- dataLength = dataLength * 300;
- }
- else
- {
- dataLength = DataTable.Rows[0][i].ToString().Length;
- dataLength = dataLength * 300;
- }
- sheet.SetColumnWidth(i, dataLength);
- }
-
- IRow row = sheet.CreateRow(0);
-
- sheet.CreateFreezePane(0, 1, 0, 1);
- ICellStyle style = book.CreateCellStyle();
- style.FillForegroundColor = HSSFColor.PaleBlue.Index;
- style.FillPattern = FillPattern.BigSpots;
- style.FillBackgroundColor = HSSFColor.LightGreen.Index;
-
- style.BorderBottom = BorderStyle.Thick;
- style.BorderLeft = BorderStyle.Thick;
- style.BorderRight = BorderStyle.Thick;
- style.BorderTop = BorderStyle.Thick;
- row.HeightInPoints = 20;
-
-
-
- for (int j = 0; j < columnNum; j++)
- {
- row.CreateCell(j);
- row.Cells[j].CellStyle = style;
- row.Cells[j].CellStyle.VerticalAlignment = VerticalAlignment.Center;
- row.Cells[j].CellStyle.Alignment = HorizontalAlignment.Center;
- row.Cells[j].SetCellValue(DataTable.Columns[j].ColumnName);
- }
-
-
- for (int i = 0; i < rowNum; i++)
- {
- IRow row1 = sheet.CreateRow(i + 1);
- row1.HeightInPoints = 20;
- for (int j = 0; j < columnNum; j++)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i][j].ToString());
- row1.GetCell(j).CellStyle.VerticalAlignment = VerticalAlignment.Center;
- }
- }
-
- book.Write(ms);
- return ms;
- }
-
-
-
-
-
- public MemoryStream DataTableToExcel1(DataTable FirstDT, DataTable DataTable, string Type, int PageSize, bool PrintWID)
- {
-
- MemoryStream ms = new MemoryStream();
-
- HSSFWorkbook book = new HSSFWorkbook();
-
- ISheet sheet = book.CreateSheet("分页");
-
- int rowNum = DataTable.Rows.Count;
- int columnNum = DataTable.Columns.Count;
- ICellStyle style = book.CreateCellStyle();
- style.VerticalAlignment = VerticalAlignment.Center;
- style.Alignment = HorizontalAlignment.Left;
- ICellStyle styleborder = book.CreateCellStyle();
- styleborder.BorderBottom = BorderStyle.Thick;
- styleborder.BorderTop = BorderStyle.Thick;
- styleborder.BorderLeft = BorderStyle.Thick;
- styleborder.BorderRight = BorderStyle.Thick;
- styleborder.VerticalAlignment = VerticalAlignment.Center;
- styleborder.Alignment = HorizontalAlignment.Left;
-
- for (int i = 0; i < columnNum; i++)
- {
- if (DataTable.Columns[i].ColumnName.Contains("客户"))
- {
- DataTable.Columns[i].ColumnName = "客户";
- }
- if (DataTable.Columns[i].ColumnName.Contains("出货单号"))
- {
- DataTable.Columns[i].ColumnName = "出货单号";
- }
- if (DataTable.Columns[i].ColumnName.Contains("型号"))
- {
- DataTable.Columns[i].ColumnName = "型号";
- }
- if (DataTable.Columns[i].ColumnName.Contains("销售日期"))
- {
- DataTable.Columns[i].ColumnName = "销售日期";
- }
- if (DataTable.Columns[i].ColumnName.ToLower().Contains("rownum"))
- {
- DataTable.Columns[i].ColumnName = "序号";
- }
- if (DataTable.Columns[i].ColumnName.ToLower().Contains("ch_code"))
- {
- DataTable.Columns[i].ColumnName = "芯片号";
- }
- if (DataTable.Columns[i].ColumnName.ToLower().Contains("pib_lotno"))
- {
- DataTable.Columns[i].ColumnName = "LotNo";
- }
- if (DataTable.Columns[i].ColumnName.ToLower().Contains("io_qty"))
- {
- DataTable.Columns[i].ColumnName = "数量";
- }
- }
-
- IRow row = sheet.CreateRow(0);
-
- sheet.CreateFreezePane(0, 1, 0, 1);
-
-
-
-
-
-
-
- row.HeightInPoints = 20;
-
-
-
- row.CreateCell(0);
- row.Cells[0].SetCellValue(" 深爱半导体有限公司芯片出货清单");
- row.GetCell(0).CellStyle = style;
-
- int PaintIndex = 1;
- int sumCount = 0;
- int totalCount = 0;
- switch (Type)
- {
- case "FixRow":
-
- BaseUtil.CleanDataTableData(FirstDT);
-
- string First_OrderCode = "";
- string First_Prspec = "";
- string First_Batch = "";
- string First_WID = "";
- for (int i = 0; i < rowNum; i++)
- {
- IRow row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- row1.HeightInPoints = 20;
-
- if (!First_OrderCode.Contains(DataTable.Rows[i]["pd_ordercode"].ToString()))
- {
- First_OrderCode += DataTable.Rows[i]["pd_ordercode"].ToString() + " ";
- }
-
- if (!First_Prspec.Contains(DataTable.Rows[i]["pr_spec"].ToString()))
- {
- First_Prspec += DataTable.Rows[i]["pr_spec"].ToString() + " ";
- }
-
- if (!First_Batch.Contains(DataTable.Rows[i]["扩散批号"].ToString()))
- {
- First_Batch += DataTable.Rows[i]["扩散批号"].ToString() + " ";
- }
-
- if (!First_WID.Contains(DataTable.Rows[i]["Wafer_ID"].ToString()))
- {
- First_WID += DataTable.Rows[i]["Wafer_ID"].ToString() + " ";
- }
- if (i / PageSize >= 1 && i % PageSize == 0)
- {
- DataRow dr = FirstDT.NewRow();
- dr["型号"] = DataTable.Rows[i]["型号"].ToString();
- dr["出货单号"] = DataTable.Rows[i]["出货单号"].ToString();
- dr["客户"] = DataTable.Rows[i]["客户"].ToString();
- dr["销售日期"] = DataTable.Rows[i]["销售日期"].ToString();
- dr["pd_ordercode"] = First_OrderCode;
- dr["pr_spec"] = First_Prspec;
- dr["ch_splitbatch"] = First_Batch;
- dr["ch_waterid"] = First_WID;
- dr["num"] = PageSize;
- dr["io_qty"] = sumCount;
- FirstDT.Rows.Add(dr);
- First_OrderCode = "";
- First_Prspec = "";
- First_Batch = "";
- First_WID = "";
- for (int j = 0; j < columnNum - 4; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("小计");
- }
- else if (j == 2)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(sumCount);
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.Cells[j].CellStyle = styleborder;
- }
- sumCount = 0;
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- }
-
- if (i % PageSize == 0 || i == rowNum - 1)
- {
-
- if (i != rowNum - 1)
- {
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["客户"].ToString());
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["出货单号"].ToString());
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.GetCell(j).CellStyle = style;
- }
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
-
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["型号"].ToString());
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["销售日期"].ToString());
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.GetCell(j).CellStyle = style;
- }
-
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- for (int j = 4; j < columnNum; j++)
- {
- row1.CreateCell(j - 4);
- row1.Cells[j - 4].CellStyle = styleborder;
- if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
- {
- row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
- }
- else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
- {
- row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
- }
- else
- row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].ColumnName);
-
- if (DataTable.Columns[j].ColumnName.ToLower().Contains("chw_itemname") || DataTable.Columns[j].ColumnName.ToLower().Contains("pr_spec") || DataTable.Columns[j].ColumnName.ToLower().Contains("pd_ordercode"))
- {
- sheet.SetColumnHidden(j - 4, true);
- }
- }
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- }
- }
-
- for (int j = 4; j < columnNum; j++)
- {
- string Data = DataTable.Rows[i][j].ToString();
- row1.CreateCell(j - 4);
- row1.Cells[j - 4].SetCellValue(Data);
- row1.GetCell(j - 4).CellStyle = styleborder;
- if (DataTable.Columns[j].ColumnName == "数量")
- {
- sumCount += int.Parse(DataTable.Rows[i][j].ToString());
- totalCount += int.Parse(DataTable.Rows[i][j].ToString());
- }
- if (DataTable.Columns[j].ColumnName == "序号")
- {
- row1.Cells[j - 4].SetCellValue(i + 1);
- }
- }
- if (i == rowNum - 1)
- {
- DataRow dr = FirstDT.NewRow();
- dr["型号"] = DataTable.Rows[i]["型号"].ToString();
- dr["出货单号"] = DataTable.Rows[i]["出货单号"].ToString();
- dr["客户"] = DataTable.Rows[i]["客户"].ToString();
- dr["销售日期"] = DataTable.Rows[i]["销售日期"].ToString();
- dr["pd_ordercode"] = First_OrderCode;
- dr["pr_spec"] = First_Prspec;
- dr["ch_splitbatch"] = First_Batch;
- dr["ch_waterid"] = First_WID;
- dr["num"] =(i% PageSize)+1;
- dr["io_qty"] = sumCount;
- FirstDT.Rows.Add(dr);
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- for (int j = 0; j < columnNum - 4; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("小计");
- }
- else if (j == 2)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(sumCount);
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.Cells[j].CellStyle = styleborder;
- }
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("备注");
- }
- else if (j == 2)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(totalCount);
- }
- else if (j == columnNum - 5)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(rowNum);
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("片");
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.Cells[j].CellStyle = style;
- }
- }
- }
- for (int i = 0; i < sheet.LastRowNum; i++)
- {
- if (i != 0)
- {
- sheet.AutoSizeColumn(i);
- sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
- }
- }
- break;
- case "BatchCode":
- string LastBatchCode = "";
- for (int i = 0; i < rowNum; i++)
- {
- IRow row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- row1.HeightInPoints = 20;
-
- if (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["扩散批号"].ToString())
- {
- for (int j = 0; j < columnNum - 4; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("小计");
- }
- else if (j == 2)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(sumCount);
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.Cells[j].CellStyle = styleborder;
- }
- sumCount = 0;
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- }
-
- if (LastBatchCode == "" || (LastBatchCode != "" && LastBatchCode != DataTable.Rows[i]["扩散批号"].ToString()) || i == rowNum - 1)
- {
- LastBatchCode = DataTable.Rows[i]["扩散批号"].ToString();
-
- if (i != rowNum - 1)
- {
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["客户"].ToString());
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["出货单号"].ToString());
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.GetCell(j).CellStyle = style;
- }
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
-
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["型号"].ToString());
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["销售日期"].ToString());
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.GetCell(j).CellStyle = style;
- }
-
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- for (int j = 4; j < columnNum; j++)
- {
- row1.CreateCell(j - 4);
- row1.Cells[j - 4].CellStyle = styleborder;
- if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
- {
- row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
- }
- else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
- {
- row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
- }
- else
- row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].ColumnName);
- if (DataTable.Columns[j].ColumnName.ToLower().Contains("chw_itemname") || DataTable.Columns[j].ColumnName.ToLower().Contains("pr_spec") || DataTable.Columns[j].ColumnName.ToLower().Contains("pd_ordercode"))
- {
- sheet.SetColumnHidden(j - 4, true);
- }
- }
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- }
- }
-
- for (int j = 4; j < columnNum; j++)
- {
- string Data = DataTable.Rows[i][j].ToString();
- row1.CreateCell(j - 4);
- row1.Cells[j - 4].SetCellValue(Data);
- row1.GetCell(j - 4).CellStyle = styleborder;
- if (DataTable.Columns[j].ColumnName == "数量")
- {
- sumCount += int.Parse(DataTable.Rows[i][j].ToString());
- totalCount += int.Parse(DataTable.Rows[i][j].ToString());
- }
- if (DataTable.Columns[j].ColumnName == "序号")
- {
- row1.Cells[j - 4].SetCellValue(i + 1);
- }
- }
- if (i == rowNum - 1)
- {
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- for (int j = 0; j < columnNum - 4; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("小计");
- }
- else if (j == 2)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(sumCount);
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.Cells[j].CellStyle = styleborder;
- }
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("备注");
- }
- else if (j == 2)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(totalCount);
- }
- else if (j == columnNum - 5)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(rowNum);
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("片");
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.Cells[j].CellStyle = style;
- }
- }
- }
- for (int i = 0; i < sheet.LastRowNum; i++)
- {
- if (i != 0)
- {
- sheet.AutoSizeColumn(i);
- sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
- }
- }
- break;
- case "BoxCode":
- string LastBoxCode = "";
- for (int i = 0; i < rowNum; i++)
- {
- IRow row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- row1.HeightInPoints = 20;
-
- if (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["盒号"].ToString())
- {
- for (int j = 0; j < columnNum - 4; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("小计");
- }
- else if (j == 2)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(sumCount);
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.Cells[j].CellStyle = styleborder;
- }
- sumCount = 0;
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- }
-
- if (LastBoxCode == "" || (LastBoxCode != "" && LastBoxCode != DataTable.Rows[i]["盒号"].ToString()) || i == rowNum - 1)
- {
- LastBoxCode = DataTable.Rows[i]["盒号"].ToString();
-
- if (i != rowNum - 1)
- {
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["客户"].ToString());
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["出货单号"].ToString());
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.GetCell(j).CellStyle = style;
- }
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
-
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["型号"].ToString());
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(DataTable.Rows[i]["销售日期"].ToString());
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.GetCell(j).CellStyle = style;
- }
-
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- for (int j = 4; j < columnNum; j++)
- {
- row1.CreateCell(j - 4);
- row1.Cells[j - 4].CellStyle = styleborder;
- if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent1")
- {
- row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname1"].ToString());
- }
- else if (DataTable.Columns[j].ColumnName.ToLower() == "chw_percent2")
- {
- row1.Cells[j - 4].SetCellValue(DataTable.Rows[i]["chw_itemname2"].ToString());
- }
- else
- row1.Cells[j - 4].SetCellValue(DataTable.Columns[j].ColumnName);
- if (DataTable.Columns[j].ColumnName.ToLower().Contains("chw_itemname") || DataTable.Columns[j].ColumnName.ToLower().Contains("pr_spec") || DataTable.Columns[j].ColumnName.ToLower().Contains("pd_ordercode"))
- {
- sheet.SetColumnHidden(j - 4, true);
- }
- }
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- }
- }
-
- for (int j = 4; j < columnNum; j++)
- {
- string Data = DataTable.Rows[i][j].ToString();
- row1.CreateCell(j - 4);
- row1.Cells[j - 4].SetCellValue(Data);
- row1.GetCell(j - 4).CellStyle = styleborder;
- if (DataTable.Columns[j].ColumnName == "数量")
- {
- sumCount += int.Parse(DataTable.Rows[i][j].ToString());
- totalCount += int.Parse(DataTable.Rows[i][j].ToString());
- }
- if (DataTable.Columns[j].ColumnName == "序号")
- {
- row1.Cells[j - 4].SetCellValue(i + 1);
- }
- }
- if (i == rowNum - 1)
- {
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- for (int j = 0; j < columnNum - 4; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("小计");
- }
- else if (j == 2)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(sumCount);
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.Cells[j].CellStyle = styleborder;
- }
- row1 = sheet.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("备注");
- }
- else if (j == 2)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(totalCount);
- }
- else if (j == columnNum - 5)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(rowNum);
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue("片");
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.Cells[j].CellStyle = style;
- }
- }
- }
- for (int i = 0; i < sheet.LastRowNum; i++)
- {
- if (i != 0)
- {
- sheet.AutoSizeColumn(i);
- sheet.SetColumnWidth(i, sheet.GetColumnWidth(i) + 1000);
- }
- }
- break;
- default:
- break;
- }
-
- sumCount = 0;
- totalCount = 0;
- PaintIndex = 1;
- ISheet sheet2 = book.CreateSheet("首页");
- row = sheet2.CreateRow(0);
- row.CreateCell(0);
- row.Cells[0].SetCellValue(" 深爱半导体有限公司芯片出货清单");
- row.GetCell(0).CellStyle = style;
- rowNum = FirstDT.Rows.Count;
- columnNum = FirstDT.Columns.Count;
- for (int i = 0; i < columnNum; i++)
- {
- if (FirstDT.Columns[i].ColumnName.ToLower().Contains("pd_ordercode"))
- {
- FirstDT.Columns[i].ColumnName = "订单号";
- }
- if (FirstDT.Columns[i].ColumnName.ToLower().Contains("pr_spec"))
- {
- FirstDT.Columns[i].ColumnName = "产品型号";
- }
- if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_splitbatch"))
- {
- FirstDT.Columns[i].ColumnName = "批号";
- }
- if (FirstDT.Columns[i].ColumnName.ToLower().Contains("ch_waterid"))
- {
- FirstDT.Columns[i].ColumnName = "片号";
- }
- if (FirstDT.Columns[i].ColumnName.ToLower().Contains("num"))
- {
- FirstDT.Columns[i].ColumnName = "片数";
- }
- if (FirstDT.Columns[i].ColumnName.ToLower().Contains("io_qty"))
- {
- FirstDT.Columns[i].ColumnName = "管芯数";
- }
- }
- for (int i = 0; i < rowNum; i++)
- {
- IRow row1 = sheet2.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- row1.HeightInPoints = 20;
-
- if (i == 0)
- {
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(FirstDT.Rows[i]["客户"].ToString());
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(FirstDT.Rows[i]["出货单号"].ToString());
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.GetCell(j).CellStyle = style;
- }
- row1 = sheet2.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
-
- for (int j = 0; j < columnNum - 3; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(FirstDT.Rows[i]["型号"].ToString());
- }
- else if (j == columnNum - 4)
- {
- row1.CreateCell(j);
- row1.Cells[j].SetCellValue(FirstDT.Rows[i]["销售日期"].ToString());
- }
- else
- {
- row1.CreateCell(j);
- }
- row1.GetCell(j).CellStyle = style;
- }
- row1 = sheet2.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
-
- for (int j = 4; j < columnNum; j++)
- {
- row1.CreateCell(j - 4);
- row1.Cells[j - 4].CellStyle = styleborder;
- row1.Cells[j - 4].SetCellValue(FirstDT.Columns[j].ColumnName);
- }
- row1 = sheet2.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- }
-
- for (int j = 4; j < columnNum; j++)
- {
- string Data = FirstDT.Rows[i][j].ToString();
- row1.CreateCell(j - 4);
- row1.Cells[j - 4].SetCellValue(Data);
- row1.GetCell(j - 4).CellStyle = styleborder;
- if (FirstDT.Columns[j].ColumnName == "片数")
- {
- sumCount += int.Parse(Data);
- }
- if (FirstDT.Columns[j].ColumnName == "管芯数")
- {
- totalCount += int.Parse(Data);
- }
- }
-
- if (i == rowNum - 1)
- {
- row1 = sheet2.CreateRow(PaintIndex);
- PaintIndex = PaintIndex + 1;
- for (int j = 0; j < columnNum - 4; j++)
- {
- if (j == 0)
- {
- row1.CreateCell(j);
- row1.Cells[j].CellStyle = styleborder;
- row1.Cells[j].SetCellValue("总计");
- }
- else if (j == columnNum - 6)
- {
- row1.CreateCell(j);
- row1.Cells[j].CellStyle = styleborder;
- row1.Cells[j].SetCellValue(sumCount);
- }
- else if (j == columnNum - 5)
- {
- row1.CreateCell(j);
- row1.Cells[j].CellStyle = styleborder;
- row1.Cells[j].SetCellValue(totalCount);
- }
- else
- {
- row1.CreateCell(j);
- row1.Cells[j].CellStyle = styleborder;
- }
- }
- }
- }
- for (int i = 0; i < sheet2.LastRowNum; i++)
- {
- if (i != 0)
- {
- sheet2.AutoSizeColumn(i);
- sheet2.SetColumnWidth(i, sheet2.GetColumnWidth(i) + 1000);
- }
- }
-
- book.Write(ms);
- return ms;
- }
- }
- }
|