gpt4 book ai didi

c# - 根据行数将一个大的 Excel 文件拆分为多个

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:13 24 4
gpt4 key购买 nike

我有一个 C# 控制台应用程序,它需要根据行数将大型 Excel 拆分为多个 Excel 文件。下面的代码显示了一个只有 51 行(包括标题列行)的源文件,但最终的源文件将有 100,000 多行。

代码试图跳过第一个(标题)行,然后应该从第 2 行复制到第 11 行,依此类推——我将目标文件设置为每个文件仅 10 行,以加快开发速度。

问题 那么如何从源 Excel 文件中复制第 2 行到第 11 行以及后续的 10 行并粘贴到多个目标 Excel 文件,以便每个目标文件都将有 10 行?

这里是几乎新写的代码。它大致基于 copying of specific range of excel cells from one worksheet to another worksheethttps://social.msdn.microsoft.com/Forums/vstudio/en-US/afd01976-63d0-4f96-9ba4-e3e2b6cf8d55/excel-with-c-how-to-specify-a-range-?forum=vsto

现在我可以写 5 个 Excel 文件了。但是第一个文件有 9 行(从第 2 行开始),而第二个文件只有 3 行,从第 10 行开始,第三个文件有 13 行,再次从第 10 行开始;最后两个文件的行越来越多,都从第 10 行开始。

那么我的 For Loop 有问题吗?或者我选择范围的方式?

     string startPath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
string filePath_source = Path.Combine(startPath, @"Source_Files\Offers_Source_Temp.xlsx");
string filePath_copiedinto = Path.Combine(startPath, @"Source_Files\ToBeCopiedInto.xlsx");
app = new Excel.Application();
app.DisplayAlerts = false;
book = app.Workbooks.Open(filePath_source);
sheet = (Excel.Worksheet)book.Worksheets.get_Item((1));
int iRowCount = sheet.UsedRange.Rows.Count;
int maxrows = 10;//change this to something like 50,000 later. 01/16/18
int maxloops = iRowCount / maxrows;
int beginrow = 2; //skipping the header row.
Excel.Application destxlApp;
Excel.Workbook destworkBook;
Excel.Worksheet destworkSheet;
Excel.Range destrange;
string srcPath;
string destPath;
//Opening of first worksheet and copying
srcPath = filePath_source;
for (int i = 1; i <= maxloops; i++) {
Excel.Range rng = (Excel.Range)sheet.Range[sheet.Cells[beginrow, 1], sheet.Cells[maxrows, 3]];
rng.Copy(Type.Missing);
//opening of the second worksheet and pasting
destPath = filePath_copiedinto;
destxlApp = new Excel.Application();
destxlApp.DisplayAlerts = false;
destworkBook = destxlApp.Workbooks.Open(destPath, 0, false);
destworkSheet = destworkBook.Worksheets.get_Item(1);
destrange = destworkSheet.Cells[1, 1];
destrange.Select();
destworkSheet.Paste(Type.Missing, Type.Missing);
destworkBook.SaveAs(startPath + "\\Output_Files\\" + beginrow + ".xlsx");
destworkBook.Close(true, null, null);
destxlApp.Quit();
beginrow = beginrow + maxrows;
string blah = null;

}

最佳答案

我建议使用 OpenXml图书馆来完成这项任务。它是无依赖性的并且支持整个 OpenXml 结构。这是一个如何读/写行的起点:

using System;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

// Open the document for editing.
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();

foreach (Row r in sheetData.Elements<Row>())
{

}
}

现在,写法非常相似:

using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Create(fileName),
SpreadsheetDocumentType.Workbook))
{
// create the workbook
spreadSheet.AddWorkbookPart();
spreadSheet.WorkbookPart.Workbook = new Workbook (); // create the worksheet
spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet = new Worksheet();

// create sheet data
spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.AppendChild(new SheetData());
// create row
spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(new Row());
}

关于c# - 根据行数将一个大的 Excel 文件拆分为多个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48302735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com