0

I am opening a tab-delimited file in Excel 2010; the delimited file displays correctly in Excel but when I do a save as, to save it as a native Excel 2010 format, the resulting saved file is empty. The resulting saved file generates no errors when debugging and saves with the correct .xlsx extension. Here is the relevant code:

            Excel.Application xlApp = null;
            Excel.Workbook xlWorkBook = null;
            Excel.Worksheet xlWorkSheet = null;
            Excel.Range range = null;
            object misValue = System.Reflection.Missing.Value;

            try
            {
                xlApp = new Excel.Application();
                xlWorkBook = xlApp.Workbooks.Add();
                xlApp.Workbooks.OpenText(inputFile, misValue, 1, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierNone, misValue,
                    misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                xlApp.Visible = true;

                xlApp.DisplayAlerts = false;
                xlWorkSheet.SaveAs(outputFile, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, false, misValue, misValue, misValue, misValue);

If I stop execution right after the xlApp.Visible = true line, I can save the file manually to the .xlsx format and it saves correctly. I figure I have something not set quite right but I'm not sure what it is.

2
Contributors
3
Replies
10
Views
3 Years
Discussion Span
Last Post by spowel4
0

Thanks but that's not working either, I end up with an empty csv file. I think I am going to have to apply some formatting too, such as bolding and underlining, autofitting, etc... so I probably need to save the file as a true Excel format.

0

I got it working, I changed this code:

xlWorkBook = xlApp.Workbooks.Add();
xlApp.Workbooks.OpenText(inputFile, misValue, 1, Excel.XlTextParsingType.xlDelimited, Excel.XlTextQualifier.xlTextQualifierNone, misValue,
    misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);

to this: xlWorkBook = xlApp.Workbooks.Open(inputFile, misValue);
and it's saving the file correctly now.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.