简体   繁体   English

如何使用 C# 代码在 Excel 中的多个电子表格中拆分数据?

[英]How do I split data across multiple spreadsheets in Excel using C# code?

I am trying to create an application that can extract a ton of data from a txt file 3000+ lines.我正在尝试创建一个可以从 3000 多行 txt 文件中提取大量数据的应用程序。

I have already filtered the data etc and I am able to write everything into the excel spreadsheet in the correct way.我已经过滤了数据等,我能够以正确的方式将所有内容写入 Excel 电子表格。 My problem is that I want to split the data over multiple sheets and limit each sheet to a specific number of data rows (The exact value is a number chosen between 100-1000)我的问题是我想将数据拆分到多个工作表上并将每个工作表限制为特定数量的数据行(确切值是在 100-1000 之间选择的数字)

I have ended up with an output that creates the correct number of sheets, but it outputs all the lines of data into all the sheets instead of splitting them up over all the sheets.我最终得到了一个创建正确数量的工作表的输出,但它将所有数据行输出到所有工作表中,而不是将它们拆分到所有工作表中。

For clarity: Example: I have 950 lines of data.为清楚起见:示例:我有 950 行数据。 Thus the output should be 100 data rows in 8 sheets and the 9th should have the last 50.因此,输出应该是 8 个工作表中的 100 个数据行,第 9 个数据行应该是最后 50 个。

What I currently get is: 950 data rows in all 9 sheets.我目前得到的是:所有 9 个工作表中有 950 个数据行。

Here is my code that controls the Excel creation.这是我控制 Excel 创建的代码。

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filename, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
                    {
                        WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
                        workbookPart.Workbook = new Workbook();
                        WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
                        worksheetPart.Worksheet = new Worksheet(new SheetData());
                        var workSheet = worksheetPart.Worksheet;
                        Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

                        for (int i = 0; i < Amount; i++)
                        {
                            
                            Sheet sheet = new Sheet()
                            {
                                Id = workbookPart.GetIdOfPart(worksheetPart),
                                SheetId = ((uint)(i + 1)),
                                Name = "Data" + (i + 1)
                            };
                            
                            sheets.Append(sheet);
                            var HeadRow = workSheet.GetFirstChild<SheetData>().AppendChild(new Row());//new Row
                            HeadRow.AppendChild(new Cell() { CellValue = new CellValue("Type"), DataType = new EnumValue<CellValues>(CellValues.String) });
                            HeadRow.AppendChild(new Cell() { CellValue = new CellValue("Description"), DataType = new EnumValue<CellValues>(CellValues.String) });
                            HeadRow.AppendChild(new Cell() { CellValue = new CellValue("Time"), DataType = new EnumValue<CellValues>(CellValues.String) });
                            HeadRow.AppendChild(new Cell() { CellValue = new CellValue("Date"), DataType = new EnumValue<CellValues>(CellValues.String) });
                            for (int j = 0 + (i * (int)OrgAmount); j < ((i + 1) * (int)OrgAmount); j++)//Rows
                            {
                                if (j == LineCount)
                                {
                                    break;
                                }

                                var row = workSheet.GetFirstChild<SheetData>().AppendChild(new Row());//new Row
                                var cell1 = row.AppendChild(new Cell());//Type
                                var cell2 = row.AppendChild(new Cell());//Description
                                var cell3 = row.AppendChild(new Cell());//Time
                                var cell4 = row.AppendChild(new Cell());//Date

                                cell1.CellValue = new CellValue(LineType[j]);
                                cell1.DataType = new EnumValue<CellValues>(CellValues.String);
                                cell2.CellValue = new CellValue(LineDesc[j]);
                                cell2.DataType = new EnumValue<CellValues>(CellValues.String);
                                cell3.CellValue = new CellValue(LineTime[j]);
                                cell3.DataType = new EnumValue<CellValues>(CellValues.String);
                                cell4.CellValue = new CellValue(LineDate[j]);
                                cell4.DataType = new EnumValue<CellValues>(CellValues.String);
                            }
                            workbookPart.Workbook.Save();
                        }
                        spreadsheetDocument.Close();
                    }

Please help me since I don't know where I messed up.请帮助我,因为我不知道我在哪里搞砸了。

I got it fixed myself in the end.最后我自己修好了。

Here is what I did:这是我所做的:

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filename, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
                    {
                        //Create workbook, workbookPart and sheets
                        WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
                        workbookPart.Workbook = new Workbook();
                        Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
                        for (int i = 0; i < Amount; i++)
                        {
                            //New worksheetPart, worksheet and sheet for each sheet to be addressed seperately
                            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
                            worksheetPart.Worksheet = new Worksheet(new SheetData());
                            var workSheet = worksheetPart.Worksheet;
                            //Dynamically create a sheet according to options chosen in form
                            Sheet sheet = new Sheet()
                            {
                                Id = workbookPart.GetIdOfPart(worksheetPart),
                                SheetId = ((uint)(i + 1)),
                                Name = "Data" + (i + 1)
                            };
                            
                            //Header row creation
                            var HeadRow = workSheet.GetFirstChild<SheetData>().AppendChild(new Row());//new Row
                            HeadRow.AppendChild(new Cell() { CellValue = new CellValue("Type"), DataType = new EnumValue<CellValues>(CellValues.String) });
                            HeadRow.AppendChild(new Cell() { CellValue = new CellValue("Description"), DataType = new EnumValue<CellValues>(CellValues.String) });
                            HeadRow.AppendChild(new Cell() { CellValue = new CellValue("Time"), DataType = new EnumValue<CellValues>(CellValues.String) });
                            HeadRow.AppendChild(new Cell() { CellValue = new CellValue("Date"), DataType = new EnumValue<CellValues>(CellValues.String) });
                            for (int j = 0 + (i * (int)OrgAmount); j < ((i + 1) * (int)OrgAmount); j++)//Rows
                            {
                                if (j == LineCount)//If final line is reached before loop end, then break loop
                                {
                                    break;
                                }
                                //Initialise new row cells
                                var row = workSheet.GetFirstChild<SheetData>().AppendChild(new Row());//new Row
                                var cell1 = row.AppendChild(new Cell());//Type
                                var cell2 = row.AppendChild(new Cell());//Description
                                var cell3 = row.AppendChild(new Cell());//Time
                                var cell4 = row.AppendChild(new Cell());//Date
                                //Write data to cells, extracted from text file
                                cell1.CellValue = new CellValue(LineType[j]);
                                cell1.DataType = new EnumValue<CellValues>(CellValues.String);
                                cell2.CellValue = new CellValue(LineDesc[j]);
                                cell2.DataType = new EnumValue<CellValues>(CellValues.String);
                                cell3.CellValue = new CellValue(LineTime[j]);
                                cell3.DataType = new EnumValue<CellValues>(CellValues.String);
                                cell4.CellValue = new CellValue(LineDate[j]);
                                cell4.DataType = new EnumValue<CellValues>(CellValues.String);
                            }
                            sheets.Append(sheet);//Append the written sheet to workbook sheets
                        }
                        workbookPart.Workbook.Save();//save workbook
                        spreadsheetDocument.Close();//Close document
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 SSIS package 将大型数据集拆分为多个 Excel 电子表格? - How to split a large dataset into multiple Excel spreadsheets using an SSIS package? 使用C#将Google Spreadsheets作为Excel获取 - Getting Google Spreadsheets as Excel using C# 如何使用C#和Visual Studio 2010在多个层之间共享服务引用类型 - How do I share Service Reference Types Across Multiple Tiers using C# and Visual Studio 2010 我可以跨多个文件拆分我的 C# 类吗? - Can I split my C# class across multiple files? C#:如何修改此代码以使用连字符分隔字符串? - C#: How do i modify this code to split the string with hyphens? 如何从 C# 加速创建 excel Excel 电子表格? - How can I speed-up creating excel Excel spreadsheets from C#? 如何使用Split()重构此C#代码? - How can I refactor this C# code using Split()? 如何使用 C# 下载 Google 电子表格? - How to download Google SpreadSheets using C#? 如何在C#中使用Regex将短语拆分为单词 - How do I split a phrase into words using Regex in C# 如何使用从SQL表读取数据并从中生成EXCEL报告的ASP.NET C#代码/程序向EXCEL单元格添加单引号? - How do I add single quote to an EXCEL cell using ASP.NET C# code/program that reads data from an SQL Table and producing an EXCEL report from it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM