Error while exporting to Excel (Corrupted file)

Asked

Viewed 1,315 times

2

I am making an application to export the result of a query sql for Excel. After the file created the time I will open it gives a corrupt file error.

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Execl files (*.xls)|*.xls";
sfd.FileName = "teste";

if (sfd.ShowDialog() == DialogResult.OK)
{
    Stream s = File.Open(sfd.FileName, FileMode.CreateNew);
    xlApp.DisplayAlerts = false;
    xlWorkBook.SaveAs(s, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel12, 
                      Missing.Value, Missing.Value, false, false, 
                      Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, 
                      Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution, 
                      true, Missing.Value, Missing.Value, Missing.Value);
    xlWorkBook.Close(true, misValue, misValue);         
}

Erro após criação do arquivoMensagem pós reabertura do arquivo

  • Would it be possible for you to make one of these corrupted files available?

  • The files are with 0b has no way to make up the file to send you, you have any idea how I can do it?

  • Then add in your question the code snippet where you add the content to the Workbook.

1 answer

2


I don’t know this library and I don’t know if there’s any specific problem using it but this code is weird.

I imagine the first parameter of xlWorkBook.SaveAs be the file name. Then pass the file name there and not a Stream. Which by the way was opened without doing anything with it and it was not closed.

What was closed was itself xlWorkBook. Probably it should not have been closed there. If it was opened elsewhere this place should be responsible for the closure. Actually, the lock should be automatic. When you try to close manually you can be a resource leak if an exception is thrown. Few programmers realize this. Test in the optimal situation and forget that if something goes wrong, you will have a bad situation and probably more difficult to identify the problem.

Without knowing the whole, if I understood the intention, I would say that this solves the problem (if it does not solve, it is likely to have some more thickening that is not apparent):

SaveFileDialog sfd = new SaveFileDialog();

sfd.Filter = "Execl files (*.xls)|*.xls";
sfd.FileName = "teste";

if (sfd.ShowDialog() == DialogResult.OK) {
    xlApp.DisplayAlerts = false;
    xlWorkBook.SaveAs(sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel12,
        Missing.Value, Missing.Value, false, false,
        Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, 
        Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution, true,
        Missing.Value, Missing.Value, Missing.Value);
    xlWorkBook.Close(true, misValue, misValue); //isto deveria estar aqui mesmo?
}

I put in the Github for future reference.

And something tells me that this xlApp.DisplayAlerts = false; is there to "hide" problems that are happening.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.