NPOI Row исчезает - PullRequest
0 голосов
/ 03 августа 2020

Я использую библиотеку NPOI для создания файлов Excel.

Первое, что я делаю, - это создаю файл, если он не существует, и добавляю к нему заголовки.

Как только я Сделал это, я пытаюсь поместить текст в ячейки ниже, но текст заголовка исчезает.

Как я могу добавить текст без исчезновения ранее написанного?

Этот код создает книгу с их заголовками:

private void CreateWB(string ruta)
{
    XSSFWorkbook workbook = new XSSFWorkbook();

    XSSFFont myFont = (XSSFFont)workbook.CreateFont();
    myFont.FontHeightInPoints = 11;
    myFont.FontName = "Tahoma";

    // Defining a border
    XSSFCellStyle borderedCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
    borderedCellStyle.SetFont(myFont);
    borderedCellStyle.BorderLeft = BorderStyle.Medium;
    borderedCellStyle.BorderTop = BorderStyle.Medium;
    borderedCellStyle.BorderRight = BorderStyle.Medium;
    borderedCellStyle.BorderBottom = BorderStyle.Medium;
    borderedCellStyle.VerticalAlignment = VerticalAlignment.Center;

    ISheet Sheet = workbook.CreateSheet("Sheet1");

    // Create the headers of the Excel file
    IRow HeaderRow = Sheet.CreateRow(0);

    // This is where the data rows start from
    int RowIndex = 1;

    // Auto sized all the affected columns
    int lastColumNum = Sheet.GetRow(0).LastCellNum;

    for (int i = 0; i <= lastColumNum; i++)
    {
        Sheet.AutoSizeColumn(i);
        GC.Collect();
    }

    // Write Excel file to disk 
    using (var fileData = new FileStream("C:\\TestExcel\\" + ruta, FileMode.Create))
    {
        workbook.Write(fileData);
    }

    FileMode modoDeCreacion = FileMode.Append;
    FileAccess mododeAcceso = FileAccess.Write;

    using (FileStream stream = new FileStream(@"C:\\TestExcel\\" + ruta, modoDeCreacion, mododeAcceso))
    {
        IWorkbook wb = new XSSFWorkbook();
        ISheet sheet = wb.CreateSheet("Sheet1");
        ICreationHelper cH = wb.GetCreationHelper();

        for (int i = 0; i < 1; i++)    // creamos una fila
        {
            if (i == 0)
            {
                IRow row = sheet.CreateRow(i);

                for (int j = 0; j < ExcelClass.Constants.cabeceras.Length; j++)     // creamos una columna
                {
                    ICell cell = row.CreateCell(j);
                    cell.SetCellValue(cH.CreateRichTextString(ExcelClass.Constants.cabeceras[j]));
                }
            }
        }

        wb.Write(stream);
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...