Code Monkey home page Code Monkey logo

Comments (5)

Bykiev avatar Bykiev commented on September 20, 2024

Hi, can you please attach original file and corrupted?

from npoi.

JeanPhilippeLux avatar JeanPhilippeLux commented on September 20, 2024

Oh, sorry.

Here you have the 2 files. Hope that will help you.

Corrupt file.xlsx
Good file.xlsx

Thank you.

from npoi.

Bykiev avatar Bykiev commented on September 20, 2024

It seems to be smth wrong with shared formulas...
Can you please also provide your code to modificate the original file?

from npoi.

JeanPhilippeLux avatar JeanPhilippeLux commented on September 20, 2024

Hi, here the code. I clean up it because there is a connection to DB

string filename = "" //the filename goes here.

XSSFWorkbook newWorkbook = new();
XSSFSheet excelSheet = new();

initializeWorkbook(ref filename, ref newWorkbook, ref excelSheet);
int rowIndex = 1;

IRow row = excelSheet.CreateRow(rowIndex);

row.CreateCell(0).SetCellValue(12002219);
row.CreateCell(1).SetCellValue((double)2.2);

XSSFFormulaEvaluator.EvaluateAllFormulaCells(newWorkbook);

using FileStream fs = new(filename, FileMode.Create, FileAccess.Write);
newWorkbook.Write(fs);
fs.Close();

PrintMyExcelFile(filename);

//this code initialize the document
private void initializeWorkbook(ref string filename, ref XSSFWorkbook newWorkbook, ref XSSFSheet excelSheet)
{
newWorkbook = new();

 using (FileStream filestream = new(filename, FileMode.Open, FileAccess.Read))
 {
     newWorkbook = new XSSFWorkbook(filestream);
     filestream.Close();
 }

 int rowIndex = 0;

 excelSheet = (XSSFSheet)newWorkbook.GetSheet("A PRODUIRE");

 if (excelSheet != null)
 {
     while (excelSheet.GetRow(rowIndex) != null)
     {
         excelSheet.GetRow(rowIndex).GetCell(0)?.SetCellValue(0);
         excelSheet.GetRow(rowIndex).GetCell(1)?.SetCellValue(0);
         rowIndex++;
     }
 }
 else
 {
     excelSheet = (XSSFSheet)newWorkbook.CreateSheet("A PRODUIRE");
 }
 

 IRow row = excelSheet.CreateRow(0);

 row.CreateCell(0).SetCellValue("ItemId");
 row.CreateCell(1).SetCellValue("Qté à produire");
 row.CreateCell(2).SetCellValue(SelectedDate.Date);

}

//this code is for printing but crash at the line : Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
private void PrintMyExcelFile(string fileName)
{
Excel.Application excelApp = new();

 Excel.Workbook wb = excelApp.Workbooks.Open(fileName,
     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
     Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
     Type.Missing, Type.Missing, Type.Missing, Type.Missing);

 Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
 for (int i = 1; i <= wb.Worksheets.Count; i++)
 {
     if (((Excel.Worksheet)wb.Worksheets[i]).Name == "MAIN")
     {
         ws = (Excel.Worksheet)wb.Worksheets[i];
         ws.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
         break;
     }
 }

 // Cleanup:
 GC.Collect();
 GC.WaitForPendingFinalizers();

 Marshal.FinalReleaseComObject(ws);

 wb.Close(false, Type.Missing, Type.Missing);
 Marshal.FinalReleaseComObject(wb);

 excelApp.Quit();
 Marshal.FinalReleaseComObject(excelApp);

}

Hope you will have the possibility to reproduce the error and fix it.

from npoi.

Bykiev avatar Bykiev commented on September 20, 2024

The bug was already fixed in #1291

from npoi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.