通过Excel文件循环并将正确的范围复制到一个单独的文件与C#

简介:今天我决定用C#做一个Excel自动化任务。 这可能是我第一次做这样的事情,所以问题很多。

任务:非常多,这个想法是以下 – 我有文件夹strPath 4个Excel文件。 我必须遍历所有这些文件,并在同一个文件夹中创build一个名为Report.xlsx的文件,并使用这些文件中的信息。

我需要的信息是第9行下面的任何东西。因此,第一行要复制的是第10行。这就是为什么我循环的第一个文件被保存为报告,并且bMakeOnce值被更改。 在第一个文件被循环并保存为之后,我开始进入else条件。 在那里,我find了XL文件的最后使用的行,我尝试将范围复制到sheetReport中。

问题:

  1. 首先 – 任何代码改进的想法;

  2. 每当我循环的文件,我得到下面的图片告诉我,每个循环文件​​已打开。 在这里输入图像说明

  3. 任何好主意如何做范围复制更好? 目前,我只是试图把复制的范围,每200 + n线,以避免一些困惑。

  4. 任何想法,为什么我没有得到任何东西在sheetReport中,除了第一个文件?

我正在使用的代码(最初,目前转到Github下面):

  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Reflection; using Excel = Microsoft.Office.Interop.Excel; using Word = Microsoft.Office.Interop.Word; class MainClass { static void Main() { string strPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\")); string[] strFiles = Directory.GetFiles(strPath); Excel.Application excel = null; bool bMakeOnce = true; int intFirstLine = 10; int intLastColumn = 50; int lastRow; int lastRowReport; Excel.Workbook wkbReport = null; string strWkbReportPath; int n = 0; foreach (string strFile in strFiles) { Console.WriteLine(strFile); Excel.Workbook wkb = null; Excel.Worksheet sheet = null; Excel.Worksheet sheetReport = null; Excel.Range rngLast = null; Excel.Range rngLastReport = null; Excel.Range rngToCopy = null; Excel.Range rngDestination = null; excel = new Excel.Application(); excel.Visible = true; wkb = OpenBook(excel, strFile); if (bMakeOnce) { bMakeOnce = false; strWkbReportPath = wkb.Path + "\\" + "Report.xlsx"; wkb.SaveAs(strWkbReportPath); wkbReport = OpenBook(excel, strWkbReportPath); } else { wkb = OpenBook(excel, strFile); sheetReport = wkbReport.Worksheets[1]; sheet = wkb.Worksheets[1]; n++; rngLastReport = sheetReport.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing); rngLast = sheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing); rngToCopy = sheet.Range[sheet.Cells[intFirstLine, 1], sheet.Cells[rngLast.Row, intLastColumn]]; int size = rngToCopy.Rows.Count; Console.WriteLine(size); rngDestination = sheetReport.Range[sheetReport.Cells[200 * n, 1], sheetReport.Cells[200 * n + size, intLastColumn]]; rngToCopy.Copy(rngDestination); //rngDestination.PasteSpecial(Excel.XlPasteType.xlPasteAll); } } wkbReport.Close(false); excel.Quit(); } public static Excel.Workbook OpenBook(Excel.Application excelInstance, string fileName, bool readOnly = false, bool editable = true, bool updateLinks = true) { Excel.Workbook book = excelInstance.Workbooks.Open( fileName, updateLinks, readOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); return book; } } 

我目前使用的代码现在在这里: https : //github.com/Vitosh/C-Sharp-Stuff/blob/master/LoopThroughFiles.cs

现在它以某种方式工作,产生我想要的:

 using System; using System.IO; using Excel = Microsoft.Office.Interop.Excel; class MainClass { static void Main() { string strPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\")); string[] strFiles = Directory.GetFiles(strPath); Excel.Application excel = null; bool bMakeOnce = true; string strReportName = "Report.xlsx"; int intFirstLine = 10; int intLastColumn = 50; int lastRow; int lastRowReport; int intTotalRows; Excel.Workbook wkbReport = null; string strWkbReportPath; int n = 0; excel = new Excel.Application(); excel.Visible = true; foreach (string strFile in strFiles) { if (strFile.Contains(strReportName)) { Console.WriteLine(strReportName + " is deleted."); File.Delete(strFile); } } foreach (string strFile in strFiles) { if (strFile.Contains(strReportName)) { continue; } Console.WriteLine(strFile); Excel.Workbook wkb = null; Excel.Worksheet sheet = null; Excel.Worksheet sheetReport = null; Excel.Range rngLastReport = null; Excel.Range rngToCopy = null; wkb = Open(excel, strFile); if (bMakeOnce) { bMakeOnce = false; strWkbReportPath = wkb.Path + "\\" + strReportName; wkb.SaveAs(strWkbReportPath); wkb.Close(); wkbReport = Open(excel, strWkbReportPath); } else { sheetReport = wkbReport.Worksheets[1]; sheet = wkb.Worksheets[1]; //lastRow = sheet.Cells[1, 3].get_End(Excel.XlDirection.xlUp).Row; intTotalRows = sheet.Rows.Count; lastRow = sheet.Cells[intTotalRows, 1].End(Excel.XlDirection.xlUp).Row; lastRowReport = sheetReport.Cells[intTotalRows, 1].End(Excel.XlDirection.xlUp).Row; //lastRowReport = sheetReport.Cells[intTotalRows, 1].get_End(Excel.XlDirection.xlUp).Row; //lastRowReport = sheetReport.Cells[intTotalRows, intTotalRows.End[Excel.XlDirection.xlUp]].Row; n++; rngToCopy = sheet.Range[sheet.Cells[intFirstLine,1],sheet.Cells[lastRow, intLastColumn]]; int size = rngToCopy.Rows.Count; rngLastReport = sheetReport.Range[sheetReport.Cells[lastRowReport+1, 1], sheetReport.Cells[lastRowReport + 1+size, intLastColumn]]; rngToCopy.Copy(rngLastReport); wkb.Close(false); } } wkbReport.Close(true); excel.Quit(); Console.WriteLine("Finished!"); } public static Excel.Workbook Open(Excel.Application excelInstance, string fileName, bool readOnly = false, bool editable = true, bool updateLinks = true) { Excel.Workbook book = excelInstance.Workbooks.Open( fileName, updateLinks, readOnly, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); return book; } //public static Excel.Workbook OpenBook(Excel.Application excelInstance, string fileName, bool readOnly = false, bool editable = true, bool updateLinks = true) //{ // Excel.Workbook book = excelInstance.Workbooks.Open( // fileName, updateLinks, readOnly, // Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, // Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing, // Type.Missing, Type.Missing); // return book; //} } 

因此,我已经把它放在这里CodeReview: https ://codereview.stackexchange.com/questions/153054/loop-through-excel-files-and-copy-correct-range-in-a-separate-file