2021-09-15 15:22:24 +05:00
|
|
|
|
using DocumentFormat.OpenXml;
|
2021-08-29 17:25:16 +05:00
|
|
|
|
using DocumentFormat.OpenXml.Packaging;
|
|
|
|
|
using DocumentFormat.OpenXml.Spreadsheet;
|
2021-07-23 14:55:31 +05:00
|
|
|
|
//using AsbSaubReport;
|
|
|
|
|
//using AutoMapper;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
using System;
|
2021-09-15 17:48:34 +05:00
|
|
|
|
using System.Collections.Generic;
|
2021-09-15 10:43:46 +05:00
|
|
|
|
using System.Linq;
|
2021-04-02 17:28:07 +05:00
|
|
|
|
|
|
|
|
|
namespace ConsoleApp1
|
|
|
|
|
{
|
2021-09-15 10:43:46 +05:00
|
|
|
|
//var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
|
|
|
|
// .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
|
|
|
|
|
// .Options;
|
|
|
|
|
//var context = new AsbCloudDbContext(options);
|
|
|
|
|
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
2021-04-02 17:28:07 +05:00
|
|
|
|
class Program
|
|
|
|
|
{
|
2021-07-28 09:47:13 +05:00
|
|
|
|
|
|
|
|
|
static void Main(/*string[] args*/)
|
2021-04-02 17:28:07 +05:00
|
|
|
|
{
|
2021-09-15 17:48:34 +05:00
|
|
|
|
var sourceExcelPaths = new List<string>
|
|
|
|
|
{
|
|
|
|
|
@"D:\excels\excel1.xlsx",
|
|
|
|
|
@"D:\excels\excel2.xlsx"
|
2021-09-16 14:34:32 +05:00
|
|
|
|
//@"D:\excels\e1.xlsx",
|
|
|
|
|
//@"D:\excels\e2.xlsx",
|
|
|
|
|
//@"D:\excels\e3.xlsx",
|
|
|
|
|
//@"D:\excels\e4.xlsx",
|
|
|
|
|
//@"D:\excels\e5.xlsx",
|
|
|
|
|
//@"D:\excels\e6.xlsx"
|
2021-09-15 17:48:34 +05:00
|
|
|
|
};
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
const string resultExcelPath = @"D:\excels\result.xlsx";
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
// result file create and init
|
|
|
|
|
using (var resultExcelFile = SpreadsheetDocument.Create(resultExcelPath,
|
|
|
|
|
SpreadsheetDocumentType.Workbook, true))
|
2021-09-15 10:43:46 +05:00
|
|
|
|
{
|
2021-09-15 17:48:34 +05:00
|
|
|
|
var workbookPart = resultExcelFile.AddWorkbookPart();
|
2021-09-15 10:43:46 +05:00
|
|
|
|
workbookPart.Workbook = new Workbook();
|
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
workbookPart.Workbook.AppendChild(new Sheets());
|
2021-09-15 10:43:46 +05:00
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
resultExcelFile.WorkbookPart.AddNewPart<SharedStringTablePart>();
|
2021-09-16 14:34:32 +05:00
|
|
|
|
resultExcelFile.WorkbookPart.SharedStringTablePart.SharedStringTable = new SharedStringTable();
|
2021-09-15 17:48:34 +05:00
|
|
|
|
resultExcelFile.WorkbookPart.AddNewPart<WorkbookStylesPart>();
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
2021-09-15 10:43:46 +05:00
|
|
|
|
workbookPart.Workbook.Save();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
foreach (var sourceExcelPath in sourceExcelPaths)
|
2021-09-15 10:43:46 +05:00
|
|
|
|
{
|
2021-09-15 17:48:34 +05:00
|
|
|
|
using var sourceExcelFile = SpreadsheetDocument.Open(sourceExcelPath, false);
|
|
|
|
|
|
|
|
|
|
using var resultExcelFile = SpreadsheetDocument.Open(resultExcelPath, true);
|
|
|
|
|
|
|
|
|
|
foreach (var sheet in sourceExcelFile.WorkbookPart.Workbook.Descendants<Sheet>())
|
2021-09-15 10:43:46 +05:00
|
|
|
|
{
|
2021-09-15 16:37:17 +05:00
|
|
|
|
// Чтобы писать на новый лист, нужен новый WorkSheetPart, WorkSheet, SheetData и Sheet.
|
|
|
|
|
// https://stackoverflow.com/questions/9120544/openxml-multiple-sheets
|
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
var resultExcelWorkSheetPart = resultExcelFile.WorkbookPart.AddNewPart<WorksheetPart>();
|
2021-09-15 16:37:17 +05:00
|
|
|
|
resultExcelWorkSheetPart.Worksheet = new Worksheet();
|
|
|
|
|
|
|
|
|
|
var resultExcelSheetData = resultExcelWorkSheetPart.
|
|
|
|
|
Worksheet.AppendChild(new SheetData());
|
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
var newSheet = new Sheet()
|
|
|
|
|
{
|
|
|
|
|
Id = resultExcelFile.WorkbookPart.GetIdOfPart(resultExcelWorkSheetPart),
|
|
|
|
|
SheetId = sheet.SheetId.Value,
|
|
|
|
|
Name = sheet.Name
|
2021-09-15 12:00:46 +05:00
|
|
|
|
};
|
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
resultExcelFile.WorkbookPart.Workbook.Sheets.Append(newSheet);
|
|
|
|
|
|
|
|
|
|
var sourceWorkSheetPart = sourceExcelFile.WorkbookPart.
|
2021-09-15 15:22:24 +05:00
|
|
|
|
GetPartById(sheet.Id) as WorksheetPart;
|
2021-09-15 16:37:17 +05:00
|
|
|
|
|
|
|
|
|
// Не самая лучшая выборка рядов текущей страницы исходного файла.
|
|
|
|
|
// Наверняка для этого есть метод.
|
2021-09-15 17:48:34 +05:00
|
|
|
|
var sourceFileCurrentListRows = sourceWorkSheetPart.Worksheet.ChildElements
|
2021-09-15 16:37:17 +05:00
|
|
|
|
.FirstOrDefault(w => w.LocalName == "sheetData").ChildElements;
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
|
|
|
|
// Числовые ячейки хранятся как есть и переносятся без проблем, а вот значения строковых ячеек хранятся
|
|
|
|
|
// в отдельной таблице. При этом в самой строковой ячейке хранится индекс этой строки в таблице
|
|
|
|
|
// (чтоб не хранить кучу одинаковых строк в документе. Что-то вроде интернирования строк).
|
|
|
|
|
// Тут как раз переносится клон этой таблицы из исходного файла в результирующий.
|
2021-09-15 16:37:17 +05:00
|
|
|
|
// Разные всяческие Part'ы можно найти тут:
|
|
|
|
|
// https://docs.microsoft.com/ru-ru/dotnet/api/documentformat.openxml.packaging.spreadsheetdocument.workbookpart?view=openxml-2.8.1
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
2021-09-16 14:34:32 +05:00
|
|
|
|
var sourceExcelStringTable = sourceExcelFile.WorkbookPart.GetPartsOfType<SharedStringTablePart>()
|
2021-09-15 17:48:34 +05:00
|
|
|
|
.FirstOrDefault().SharedStringTable;
|
2021-09-16 14:34:32 +05:00
|
|
|
|
|
|
|
|
|
//resultExcelFile.WorkbookPart.SharedStringTablePart.SharedStringTable.Load(sourceExcelFile.WorkbookPart.SharedStringTablePart);
|
|
|
|
|
//sourceExcelStringTable.CloneNode(true) as SharedStringTable;
|
2021-09-15 15:22:24 +05:00
|
|
|
|
|
|
|
|
|
// Аналогично переносим таблицу стилей ячеек (жирный шрифт и прочее).
|
|
|
|
|
// Иначе ячейки переносятся абсолютно обычными, без всякой стилизации.
|
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
var stylesSheet = sourceExcelFile.WorkbookPart.GetPartsOfType<WorkbookStylesPart>()
|
|
|
|
|
.FirstOrDefault().Stylesheet;
|
|
|
|
|
resultExcelFile.WorkbookPart.WorkbookStylesPart.Stylesheet =
|
2021-09-15 15:22:24 +05:00
|
|
|
|
stylesSheet.CloneNode(true) as Stylesheet;
|
|
|
|
|
|
|
|
|
|
// Клонируем ряды в новый файл (без клонирования они не вытаскиваются из общего дерева)
|
2021-09-15 12:00:46 +05:00
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
foreach (var row in sourceFileCurrentListRows)
|
2021-09-16 14:34:32 +05:00
|
|
|
|
{
|
|
|
|
|
var newCells = new List<Cell>();
|
|
|
|
|
|
|
|
|
|
foreach(var c in row.ChildElements)
|
|
|
|
|
{
|
|
|
|
|
var cell = (Cell)c;
|
|
|
|
|
if (cell.DataType is not null && cell.DataType.Value == CellValues.SharedString)
|
|
|
|
|
{
|
|
|
|
|
var cellValue = sourceExcelStringTable.ElementAt(int.Parse(cell.InnerText)).InnerText;
|
|
|
|
|
|
|
|
|
|
newCells.Add(new Cell {
|
|
|
|
|
CellReference = cell.CellReference,
|
|
|
|
|
DataType = CellValues.String,
|
|
|
|
|
CellValue = new CellValue(cellValue)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
newCells.Add(cell.CloneNode(true) as Cell);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newRow = row.CloneNode(true);
|
|
|
|
|
|
|
|
|
|
newRow.RemoveAllChildren();
|
|
|
|
|
newRow.Append(newCells);
|
|
|
|
|
resultExcelSheetData.AppendChild(newRow);
|
|
|
|
|
}
|
2021-09-15 10:43:46 +05:00
|
|
|
|
}
|
2021-09-15 12:00:46 +05:00
|
|
|
|
}
|
2021-08-29 17:25:16 +05:00
|
|
|
|
|
2021-09-15 17:48:34 +05:00
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
2021-04-08 17:54:02 +05:00
|
|
|
|
Console.WriteLine("Done. Press any key to quit.");
|
|
|
|
|
Console.ReadKey();
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
2021-04-07 18:01:56 +05:00
|
|
|
|
|
2021-04-02 17:28:07 +05:00
|
|
|
|
}
|
|
|
|
|
}
|