-2

I tried to debug this code, but I didn't manage. Do any of you have any idea why my script creates a corrupted XLS file?

string strCaleSalvareTest = @"C:\Users\andrei.tudor\Documents\TipMacheta.xls";
HSSFWorkbook wbXLS;
strEr = "Er";
try
{
    fsXLSCitire = new FileStream(strCaleSalvareTest, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
    wbXLS = new HSSFWorkbook(fsXLSCitire);

    strEr = string.Empty;
}
catch (Exception ex)
{
    strEr = ex.Message;
}

When I try to run this, it jumps from wbXLS creation to the catch exception block.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Spoukey
  • 15
  • 6
  • What is the exception? You should always include relevant information like that in your question. – mason Dec 09 '16 at 16:26
  • this isn't really the place to be saying why doesn't my code work, take a look at the documentation here https://npoi.codeplex.com/documentation and how to ask a question that's not off topic and wont get your questions voted to be closed http://stackoverflow.com/help/mcve – Simon Price Dec 09 '16 at 16:27
  • there is no exception , the excel is created but is corupted and is also empty – Spoukey Dec 09 '16 at 16:30
  • Your question says at the end that 'it jump from wbXLS creation to the catch exception'... – user469104 Dec 09 '16 at 16:37

1 Answers1

0

You're getting an exception because you are creating a new FileStream for writing (FileAccess.Write) and passing it to the constructor of HSSFWorkbook which is expecting to be able to read from the stream. The file is corrupt because the FileStream is creating the file, but nothing is ever written to it.

If you're just trying to create a new blank workbook and save it to a file, you can do that as shown below. Note that you need to add at least one worksheet to the new workbook, or you will still generate a corrupt file.

// Create a new workbook with an empty sheet
HSSFWorkbook wbXLS = new HSSFWorkbook();
ISheet sheet = wbXLS.CreateSheet("Sheet1");

// Write the workbook to a file
string fileName = @"C:\Users\andrei.tudor\Documents\TipMacheta.xls";
using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
    wbXLS.Write(stream);
}

If you're trying to read an existing workbook OR create a new one if it doesn't exist, you need to do something like this:

string fileName = @"C:\Users\andrei.tudor\Documents\TipMacheta.xls";
HSSFWorkbook wbXLS;

try
{
    // Try to open and read existing workbook
    using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    {
        wbXLS = new HSSFWorkbook(stream);
    }
}
catch (FileNotFoundException)
{
    // Create a new workbook with an empty sheet
    wbXLS = new HSSFWorkbook();
    wbXLS.CreateSheet("Sheet1");
}

ISheet sheet = wbXLS.GetSheetAt(0);  // Get first sheet

// ... 

// Write workbook to file
using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
    wbXLS.Write(stream);
}
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300