OpenXML code writing only numeric cells from source Excel file

This commit is contained in:
KharchenkoVV 2021-09-15 12:00:46 +05:00
parent 41cacf62ff
commit 22827b8dcf

View File

@ -25,72 +25,60 @@ namespace ConsoleApp1
static void Main(/*string[] args*/) static void Main(/*string[] args*/)
{ {
// source files
using var firstExcel = SpreadsheetDocument.Open(@"D:\excels\excel1.xlsx", true); using var firstExcel = SpreadsheetDocument.Open(@"D:\excels\excel1.xlsx", true);
using var secondExcel = SpreadsheetDocument.Open(@"D:\excels\excel2.xlsx", true); using var secondExcel = SpreadsheetDocument.Open(@"D:\excels\excel2.xlsx", true);
using (var resultExcel = SpreadsheetDocument.Create(@"D:\excels\result.xlsx", SpreadsheetDocumentType.Workbook, true))
// init destination file
using var resultExcel = SpreadsheetDocument.Create(@"D:\excels\result.xlsx", SpreadsheetDocumentType.Workbook);
WorkbookPart resultExcelWorkbookpart = resultExcel.AddWorkbookPart();
resultExcelWorkbookpart.Workbook = new Workbook
{
Sheets = new Sheets()
};
var resultExcelWorksheetPart = resultExcelWorkbookpart.AddNewPart<WorksheetPart>();
resultExcelWorksheetPart.Worksheet = new Worksheet(new SheetData());
resultExcelWorkbookpart.Workbook.Save();
resultExcel.Close();
foreach (var sheet in firstExcel.WorkbookPart.Workbook.Descendants<Sheet>())
{
//resultExcel.WorkbookPart.Workbook.Sheets.Append(sheet.CloneNode(true)); // Clone empty sheet
var firstExcelWorkSheetPart = firstExcel.WorkbookPart.GetPartById(sheet.Id) as WorksheetPart; // get rows from source
var rows = firstExcelWorkSheetPart.Worksheet.Descendants<Row>(); // Rows
}
//resultExcel.Close();
using (var document = SpreadsheetDocument.Create(@"D:\excels\result123123.xlsx", SpreadsheetDocumentType.Workbook, true))
{ {
// Add a WorkbookPart to the document. // Add a WorkbookPart to the document.
var workbookPart = document.AddWorkbookPart(); var workbookPart = resultExcel.AddWorkbookPart();
workbookPart.Workbook = new Workbook(); workbookPart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart. // Add a WorksheetPart to the WorkbookPart.
var worksheetPart1 = workbookPart.AddNewPart<WorksheetPart>(); var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart1.Worksheet = new Worksheet(); worksheetPart.Worksheet = new Worksheet();
var sheets = workbookPart.Workbook.AppendChild(new Sheets()); var sheets = workbookPart.Workbook.AppendChild(new Sheets());
//var sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Test Sheet" };
var sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart1), SheetId = 1, Name = "Test Sheet" }; //sheets.Append(sheet);
sheets.Append(sheet);
workbookPart.Workbook.Save(); workbookPart.Workbook.Save();
} }
using (var document = SpreadsheetDocument.Open(@"D:\excels\result123123.xlsx", true)) using (var resultExcel = SpreadsheetDocument.Open(@"D:\excels\result.xlsx", true))
{ {
var worksheetPart1 = document.WorkbookPart.WorksheetParts.First(); var resultExcelWorkSheetPart = resultExcel.WorkbookPart.WorksheetParts.First();
var sheetData1 = worksheetPart1.Worksheet.AppendChild(new SheetData()); var resultExcelSheetData = resultExcelWorkSheetPart.Worksheet.AppendChild(new SheetData());
for (uint i = 1; i < 10; i++) foreach (var sheet in firstExcel.WorkbookPart.Workbook.Descendants<Sheet>())
{ {
var row1 = new Row(); var newSheet = new Sheet() {
row1.Append(new Cell { CellValue = new CellValue(i.ToString()) }); Id = resultExcel.WorkbookPart.GetIdOfPart(resultExcelWorkSheetPart),
sheetData1.AppendChild(row1); SheetId = sheet.SheetId.Value,
Name = sheet.Name
};
resultExcel.WorkbookPart.Workbook.Sheets.Append(newSheet);
var firstExcelWorkSheetPart = firstExcel.WorkbookPart.GetPartById(sheet.Id) as WorksheetPart; // get rows from source
var firstExcelRows = firstExcelWorkSheetPart.Worksheet.Descendants<Row>(); // retrieved rows
//var firstExcelRows = sheet.Descendants<Row>();
foreach (var row in firstExcelRows)
{
resultExcelSheetData.AppendChild(row.CloneNode(true));
}
} }
//for (uint i = 1; i < 10; i++) // fills cells fine
//{
// var row = new Row();
// row.Append(new Cell { CellValue = new CellValue(i.ToString()) });
// sheetData.AppendChild(row.CloneNode(true));
//}
} }
//resultExcel.Close();
Console.WriteLine("Done. Press any key to quit."); Console.WriteLine("Done. Press any key to quit.");
Console.ReadKey(); Console.ReadKey();
} }