public void completionData(string szWorkFileFullPath, ref System.Windows.Forms.RichTextBox rInfoOutPut) { OperatorFunc rOperator = new OperatorFunc(writeData); //string szName = Global.Tools.getNameFromFullPath(szWorkFileFullPath); ExcelOperator.excelWrite(szWorkFileFullPath, rOperator, ref rInfoOutPut); }
public void completionData(string szWorkFileFullPath, string szResultFileFolder, ref System.Windows.Forms.RichTextBox rInfoOutPut) { //打开第一个文件, if (m_szResultFolderPath != szResultFileFolder) { m_szResultFolderPath = szResultFileFolder; } OperatorFunc rOperator = new OperatorFunc(writeData); string szName = Global.Tools.getNameFromFullPath(szWorkFileFullPath); ExcelOperator.excelWrite(szWorkFileFullPath, szResultFileFolder + szName, rOperator, ref rInfoOutPut); }
public static void excelWrite(string szPath, string szResultPath, OperatorFunc rOperatorFunc, ref System.Windows.Forms.RichTextBox rInfoOutput) { string szExcelFilePath = szPath.Trim(); Excel.Application excel = new Excel.Application(); Excel.Workbooks wb = excel.Workbooks; excel.Visible = false;//设置调用引用的 Excel文件是否可见 excel.Application.DisplayAlerts = false; //wb = excel.Workbooks.Open(ExcelFilePath); Excel.Workbook rWbk = wb.Add(szExcelFilePath); Excel.Sheets rWorkSheets = rWbk.Worksheets; try {//每个工作表都查 索引从1开始 int[] rColNum = new int[(int)OperatorHead.HeadMax]; for (int index = 1; index <= rWorkSheets.Count; ++index) { Excel.Worksheet ws = (Excel.Worksheet)rWorkSheets[index]; int rowCount = 0; //有效行,索引从1开始 rowCount = ws.UsedRange.Rows.Count; //赋值有效行 bool bIsHead = false; bool bIsEnd = true; for (int i = 1; i <= rowCount; i++) // { //将行中数据交给 代理处理 string[] rUseFulContent = new string[(int)OperatorHead.HeadMax]; int nColCount = ws.UsedRange.Columns.Count; for (int nLoopCount = ws.UsedRange.Column; nLoopCount <= nColCount; ++nLoopCount) {//循环一行中的每一列 if (ws.Cells[i, nLoopCount].Value == null) { continue; } string szContent = ws.Cells[i, nLoopCount].Value.ToString().Trim(); bIsEnd = bIsEnd && szContent == ""; if (szContent == "") { continue; } if (checkIsHead(szContent)) {//代表当前行中存在表头 if (!bIsHead) { bIsHead = true; clearHeadindexArr(ref rColNum); } //记录行列标记 setHeadindex(szContent, nLoopCount, ref rColNum); } string szAddress = ws.Cells[i, nLoopCount].Address; } if (!bIsHead && headValidaion(rColNum[0], rColNum[1], rColNum[2], rColNum[3])) { if (bIsEnd) { clearHeadindexArr(ref rColNum); continue; } for (int nUsefulIndex = 0; nUsefulIndex < rColNum.Length; nUsefulIndex += 2) { int nColName = rColNum[nUsefulIndex]; int nColID = rColNum[nUsefulIndex + 1]; if (0 != nColID && 0 != nColName) { string szName; string szID; if (ws.Cells[i, nColName].Value == null) { szName = ""; } else { szName = ws.Cells[i, nColName].Value.ToString().Trim(); } if (ws.Cells[i, nColID].Value == null) { szID = ""; } else { szID = ws.Cells[i, nColID].Value.ToString().Trim(); } if ((szID == "" && szName == "") || (szID != "" && szName != "")) {//都不是空,或者都是空都不需要填写 break; } if (rOperatorFunc(ref szName, ref szID, nUsefulIndex > 0 ? false : true)) { ws.Cells[i, nColName] = szName; ws.Cells[i, nColID] = szID; } } } } bIsHead = false; } } } catch (Exception ex) { rInfoOutput.SelectionColor = Color.Red; rInfoOutput.AppendText(ex.ToString() + '\n'); } finally { excelSaveClose(szPath, szResultPath, excel, rWbk); } }