简体   繁体   中英

Convert from Excel to CSV via asp.net (C#) under IIS

I have simple procedure for converting xlsx to csv.

public static void ConvertExcelToCsv(string source, string destination, int sheetNumber = 1)
{

    if (File.Exists(destination)) File.Delete(destination);

    Excel.Application xl = new Excel.Application();

    xl.DisplayAlerts = false;
    Excel.Workbook workbook = xl.Workbooks.Open(source);
    // workbook.Close(true);
    workbook.SaveAs(destination, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV);
    object misValue = System.Reflection.Missing.Value;

    if (workbook != null)
    {
        workbook.Close(false, Type.Missing, Type.Missing);
        xl.Workbooks.Close();
        Marshal.ReleaseComObject(workbook);
    }


    xl.Quit();
    GC.Collect();
    Marshal.FinalReleaseComObject(xl);
}

Everything works fine on my local machine and also local IIS. But after uploading onto web server seems to don't work. Everything what it does is processing about 3 mins and then time out.

I also allowed permissions in Component

Services -> Computers -> My Computer -> DCOM Config -> Microsoft Excel Application -> IIS_IUSRS

and also created Desktop folders

C:\\Windows\\SysWOW64\\config\\systemprofile\\

and also in 32bit :-)

So right now, I have no idea, why it doesn't work. Do you have similar experience with this or can you provide some hint what should I do, please.

What is the exact error that you receive? Check your IIS logs and the Application event log for specific errors.

A couple things to check for:

  1. Make sure Enable 32-bit applications is set to True for your app pool
  2. If you have not installed Excel on your web server you need to copy the Interop assemblies when you publish your site. To do this set Copy Local to True for the reference to the Interop.
  3. Ensure your app has the appropriate permissions to the path referred to by source and destination

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM