forked from ddrilling/AsbCloudServer
OpenXML code writing only numeric cells from source Excel file
This commit is contained in:
parent
41cacf62ff
commit
22827b8dcf
@ -25,72 +25,60 @@ namespace ConsoleApp1
|
||||
static void Main(/*string[] args*/)
|
||||
{
|
||||
|
||||
// source files
|
||||
using var firstExcel = SpreadsheetDocument.Open(@"D:\excels\excel1.xlsx", true);
|
||||
using var secondExcel = SpreadsheetDocument.Open(@"D:\excels\excel2.xlsx", 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))
|
||||
using (var resultExcel = SpreadsheetDocument.Create(@"D:\excels\result.xlsx", SpreadsheetDocumentType.Workbook, true))
|
||||
{
|
||||
// Add a WorkbookPart to the document.
|
||||
var workbookPart = document.AddWorkbookPart();
|
||||
var workbookPart = resultExcel.AddWorkbookPart();
|
||||
workbookPart.Workbook = new Workbook();
|
||||
|
||||
// Add a WorksheetPart to the WorkbookPart.
|
||||
var worksheetPart1 = workbookPart.AddNewPart<WorksheetPart>();
|
||||
worksheetPart1.Worksheet = new Worksheet();
|
||||
var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
|
||||
worksheetPart.Worksheet = new Worksheet();
|
||||
var sheets = workbookPart.Workbook.AppendChild(new Sheets());
|
||||
|
||||
|
||||
var sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart1), SheetId = 1, Name = "Test Sheet" };
|
||||
sheets.Append(sheet);
|
||||
//var sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Test Sheet" };
|
||||
//sheets.Append(sheet);
|
||||
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 sheetData1 = worksheetPart1.Worksheet.AppendChild(new SheetData());
|
||||
var resultExcelWorkSheetPart = resultExcel.WorkbookPart.WorksheetParts.First();
|
||||
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();
|
||||
row1.Append(new Cell { CellValue = new CellValue(i.ToString()) });
|
||||
sheetData1.AppendChild(row1);
|
||||
var newSheet = new Sheet() {
|
||||
Id = resultExcel.WorkbookPart.GetIdOfPart(resultExcelWorkSheetPart),
|
||||
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.ReadKey();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user