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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.