DD.WellWorkover.Cloud/ConsoleApp1/Program.cs

88 lines
3.4 KiB
C#

using AsbCloudApp.Data;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services;
using AsbCloudInfrastructure.Services.Cache;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
//using AsbSaubReport;
//using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.IO;
using System.Linq;
namespace ConsoleApp1
{
//var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
// .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
// .Options;
//var context = new AsbCloudDbContext(options);
class Program
{
static void Main(/*string[] args*/)
{
using var firstExcel = SpreadsheetDocument.Open(@"D:\excels\excel1.xlsx", true);
using var secondExcel = SpreadsheetDocument.Open(@"D:\excels\excel2.xlsx", true);
using (var resultExcel = SpreadsheetDocument.Create(@"D:\excels\result.xlsx", SpreadsheetDocumentType.Workbook, true))
{
// Add a WorkbookPart to the document.
var workbookPart = resultExcel.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
var sheets = workbookPart.Workbook.AppendChild(new Sheets());
//var sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Test Sheet" };
//sheets.Append(sheet);
workbookPart.Workbook.Save();
}
using (var resultExcel = SpreadsheetDocument.Open(@"D:\excels\result.xlsx", true))
{
var resultExcelWorkSheetPart = resultExcel.WorkbookPart.WorksheetParts.First();
var resultExcelSheetData = resultExcelWorkSheetPart.Worksheet.AppendChild(new SheetData());
foreach (var sheet in firstExcel.WorkbookPart.Workbook.Descendants<Sheet>())
{
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();
}
}
}