无法更新 Windows Server 2008 R2 上部署的 WCF 中的 Excel (.xls) 文件

发布于 2024-11-12 19:55:03 字数 2492 浏览 5 评论 0原文

当我尝试通过 WCF 更新现有 Excel 文件时,此问题特定于一个案例。请注意,我能够读取 Excel 文件,并且仅当我尝试更新任何 Excel 文件时才会出现此问题。此外,这个更新逻辑在我的开发环境(WinXP,生产环境是 Windows Server 2008 R2)上运行得非常好。

我也尝试过 Borgon 博客 (http://hopschwiiz.blogspot.com/2011/02/automating-excel-2007-on-windows-server.html) 中提到的步骤,但没有任何运气。

我正在使用 .Net 3.5、SQL Server 2008 和 SQL Server 2008。 SL 3.0。

按照要求添加了代码...

string[] strArray;
string fileName = null;
System.Array myvalues = null;
Microsoft.Office.Interop.Excel.Application ExcelObj = null;

try
{
    fileName = System.Configuration.ConfigurationManager.AppSettings["FileLocation"].ToString();
    fileName += "JobDetails.xls";

    ExcelObj = new Microsoft.Office.Interop.Excel.Application();
    Excel.Workbook theWorkbook = (Excel.Workbook)ExcelObj.Workbooks.Open(fileName, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "", true, false, 0, false, true, false);
    Excel.Sheets sheets = theWorkbook.Worksheets;
    for (int sheetNum = 1; sheetNum <= sheets.Count; sheetNum++)
    {
        Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(sheetNum);

        for (int i = 8; i <= 50; i++)
            {
                Excel.Range range = worksheet.get_Range("A" + i.ToString(), "AI" + i.ToString());
                myvalues = (System.Array)range.Cells.get_Value(Type.Missing);
                strArray = ConvertToStringArray(myvalues);

                if (strArray[1].Equals("PSA Id") && strArray[2].Equals("Member Name") && strArray[3].Equals("Project Name"))
                {
                    int j = i;
                    worksheet.Cells[j, 5] = Month; // Updated Month in the excel file.
                    foreach (MemberShift item in listOfJobPlan)
                    {
                        j++;
                        worksheet.Cells[j, 2] = item.MemberID.ToString("D" + 6);
                        worksheet.Cells[j, 3] = item.MemberName;
                        worksheet.Cells[j, 4] = inGroupName;
                    }
                    break;
                }
            }
    }
}
catch (Exception ex)
{
    string Log = "DataService";
    if ((!(EventLog.SourceExists(Log))))
        EventLog.CreateEventSource(Log, Log);
    EventLog logEntry = new EventLog();
    logEntry.Source = Log;
    logEntry.WriteEntry("Message : " + ex.Message + "\n StackTrace : " + ex.StackTrace, EventLogEntryType.Error);
    return false;
}
finally
{
    ExcelObj.Workbooks.Close();
}

return true;

This issue is specific to a case, when i am trying to update an existing excel file via WCF. Please note that i am able to read excel files and the issue only occurs when i try to update any excel file. Also, this update logic works perfectly fine on my development environment (WinXP where as production environment is Windows server 2008 R2).

I have tried the steps mentioned in Borgon's Blog (http://hopschwiiz.blogspot.com/2011/02/automating-excel-2007-on-windows-server.html) as well but without any luck.

I am using .Net 3.5, SQL Server 2008 & SL 3.0.

As requested have added the codes...

string[] strArray;
string fileName = null;
System.Array myvalues = null;
Microsoft.Office.Interop.Excel.Application ExcelObj = null;

try
{
    fileName = System.Configuration.ConfigurationManager.AppSettings["FileLocation"].ToString();
    fileName += "JobDetails.xls";

    ExcelObj = new Microsoft.Office.Interop.Excel.Application();
    Excel.Workbook theWorkbook = (Excel.Workbook)ExcelObj.Workbooks.Open(fileName, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "", true, false, 0, false, true, false);
    Excel.Sheets sheets = theWorkbook.Worksheets;
    for (int sheetNum = 1; sheetNum <= sheets.Count; sheetNum++)
    {
        Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(sheetNum);

        for (int i = 8; i <= 50; i++)
            {
                Excel.Range range = worksheet.get_Range("A" + i.ToString(), "AI" + i.ToString());
                myvalues = (System.Array)range.Cells.get_Value(Type.Missing);
                strArray = ConvertToStringArray(myvalues);

                if (strArray[1].Equals("PSA Id") && strArray[2].Equals("Member Name") && strArray[3].Equals("Project Name"))
                {
                    int j = i;
                    worksheet.Cells[j, 5] = Month; // Updated Month in the excel file.
                    foreach (MemberShift item in listOfJobPlan)
                    {
                        j++;
                        worksheet.Cells[j, 2] = item.MemberID.ToString("D" + 6);
                        worksheet.Cells[j, 3] = item.MemberName;
                        worksheet.Cells[j, 4] = inGroupName;
                    }
                    break;
                }
            }
    }
}
catch (Exception ex)
{
    string Log = "DataService";
    if ((!(EventLog.SourceExists(Log))))
        EventLog.CreateEventSource(Log, Log);
    EventLog logEntry = new EventLog();
    logEntry.Source = Log;
    logEntry.WriteEntry("Message : " + ex.Message + "\n StackTrace : " + ex.StackTrace, EventLogEntryType.Error);
    return false;
}
finally
{
    ExcelObj.Workbooks.Close();
}

return true;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ左 2024-11-19 19:55:03

检查网站以什么用户身份运行,然后确保该用户具有编辑文件的适当权限。

Check to see what user the Website is running as and then make sure that user has the appropriate permissions to edit the files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文