Open SDK在web服务器上运行时不能打开Excel文件,但在本地主机上工作
本文关键字:文件 工作 主机 Excel web SDK 服务器 运行时 Open 不能 | 更新日期: 2023-09-27 18:11:44
我有一个c# Web表单应用程序,我想使用Open XML SDK 2.5将gridview的内容导出到excel。这工作完美,当我在本地主机运行,但它不工作,当我移动我的应用程序到UAT web服务器,我没有得到任何错误。我认为这是一个前提错误,但不确定。谢谢你,
DocumentFormat.OpenXml.Packaging.SpreadsheetDocument spreadsheet;
DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet;
System.IO.StreamReader styleXmlReader;
string styleXml;
spreadsheet = Excel.CreateWorkbook(workbookName);
if (spreadsheet == null)
{
return;
}
if (createStylesInCode)
{
Excel.AddBasicStyles(spreadsheet);
}
else
{
using (styleXmlReader = new
System.IO.StreamReader("PredefinedStyles.xml"))
{
styleXml = styleXmlReader.ReadToEnd();
Excel.AddPredefinedStyles(spreadsheet, styleXml);
}
}
Excel.AddSharedString(spreadsheet, "Shared string");
Excel.AddWorksheet(spreadsheet, "Test 1");
Excel.AddWorksheet(spreadsheet, "Test 2");
worksheet =
spreadsheet.WorkbookPart.WorksheetParts.First().Worksheet;
// Headings
Excel.SetCellValue(spreadsheet, worksheet, 1, 1, "QMS Area",
true);
Excel.SetCellValue(spreadsheet, worksheet, 2, 1, "Division
Owner", true);
Excel.SetCellValue(spreadsheet, worksheet, 3, 1, "Relevant
Divisions", true);
Excel.SetCellValue(spreadsheet, worksheet, 4, 1, "Audit Team",
true);
Excel.SetCellValue(spreadsheet, worksheet, 5, 1, "Audit Date",
true);
int _rowCount = 2;
foreach (var audit in _auditDetails)
{
// Set Values
Excel.SetCellValue(spreadsheet, worksheet, 1,
(uint)_rowCount, audit.QmsArea, true);
Excel.SetCellValue(spreadsheet, worksheet, 2,
(uint)_rowCount, audit.DivisionOwner ?? "", true);
Excel.SetCellValue(spreadsheet, worksheet, 3,
(uint)_rowCount, audit.RelevantDivision ?? "", true);
Excel.SetCellValue(spreadsheet, worksheet, 4,
(uint)_rowCount, audit.AuditTeam ?? "", true);
Excel.SetCellValue(spreadsheet, worksheet, 5,
(uint)_rowCount, audit.AuditDate.ToString() ?? "", true);
_rowCount++;
}
// Set column widths
Excel.SetColumnWidth(worksheet, 1, 15);
Excel.SetColumnWidth(worksheet, 2, 20);
worksheet.Save();
spreadsheet.Close();
System.Diagnostics.Process.Start(workbookName);
我会检查保存excel文件的服务器上的文件夹权限。你需要确保你的web应用程序对该文件夹有读/写权限。
它在你的本地机器上工作,而不是在托管环境中工作,这使我认为文件夹权限可能是问题。