dotnet new console
dotnet add package NPOI
dotnet run
using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; List<string> Cols = [ "№", "Номенклатура", "Кількість", "Ціна", "Сума" ]; List<Records> Records = [ new Records(1, "Хліб", 1, 25, 25), new Records(2, "Батон", 2, 30, 60), new Records(3, "Булка", 1, 40, 40) ]; static void CreateCell(IRow CurrentRow, int CellIndex, string Value, XSSFCellStyle Style) { ICell Cell = CurrentRow.CreateCell(CellIndex); Cell.SetCellValue(Value); Cell.CellStyle = Style; } IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("Звіт"); //Header { XSSFFont font = (XSSFFont)workbook.CreateFont(); font.FontHeightInPoints = 11; font.FontName = "Arial"; font.IsBold = true; XSSFCellStyle cellStyle = (XSSFCellStyle)workbook.CreateCellStyle(); cellStyle.SetFont(font); cellStyle.BorderLeft = BorderStyle.Dashed; cellStyle.BorderTop = BorderStyle.Dashed; cellStyle.BorderRight = BorderStyle.Dashed; cellStyle.BorderBottom = BorderStyle.Dashed; cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; IRow row = sheet.CreateRow(0); for (int i = 0; i < Cols.Count; i++) CreateCell(row, i, Cols[i], cellStyle); } //Body { XSSFFont font = (XSSFFont)workbook.CreateFont(); font.FontHeightInPoints = 10; font.FontName = "Arial"; XSSFCellStyle cellStyle = (XSSFCellStyle)workbook.CreateCellStyle(); cellStyle.SetFont(font); for (int i = 0; i < Records.Count; i++) { IRow row = sheet.CreateRow(i + 1); Records record = Records[i]; CreateCell(row, 0, record.НомерРядка.ToString(), cellStyle); CreateCell(row, 1, record.Номенклатура, cellStyle); CreateCell(row, 2, record.Кількість.ToString(), cellStyle); CreateCell(row, 3, record.Ціна.ToString(), cellStyle); CreateCell(row, 4, record.Сума.ToString(), cellStyle); } } for (int i = 0; i < Cols.Count; i++) sheet.AutoSizeColumn(i); //sheet.SetAutoFilter(new NPOI.SS.Util.CellRangeAddress(0, Records.Count - 1, 0, Cols.Count - 1)); GC.Collect(); using FileStream stream = new FileStream("Excel.xlsx", FileMode.Create, FileAccess.Write); workbook.Write(stream); record Records(int НомерРядка, string Номенклатура, int Кількість, float Ціна, float Сума);
© accounting.org.ua - 2024