简体   繁体   English

在IIS下通过asp.net(C#)从Excel转换为CSV

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

I have simple procedure for converting xlsx to csv. 我有将xlsx转换为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. 在我的本地计算机和本地IIS上,一切正常。 But after uploading onto web server seems to don't work. 但是上传到Web服务器后似乎不起作用。 Everything what it does is processing about 3 mins and then time out. 它所做的所有事情都要处理大约3分钟,然后超时。

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 :-) 也是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. 检查IIS日志和应用程序事件日志中的特定错误。

A couple things to check for: 需要检查的几件事:

  1. Make sure Enable 32-bit applications is set to True for your app pool 确保您的应用程序池的“启用32位应用程序”设置为True
  2. If you have not installed Excel on your web server you need to copy the Interop assemblies when you publish your site. 如果尚未在Web服务器上安装Excel,则在发布站点时需要复制Interop程序集。 To do this set Copy Local to True for the reference to the Interop. 为此,将“复制本地”设置为True以引用Interop。
  3. Ensure your app has the appropriate permissions to the path referred to by source and destination 确保您的应用对sourcedestination引用的路径具有适当的权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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