我有一个C#Web表单应用程序,我想使用Open XML SDK 2.5将gridview的内容导出到excel。当我在localhost中运行时这很好用,但是当我将我的应用程序移动到UAT网络服务器时它没有工作,我没有收到任何错误。我认为这是一个提示错误但不确定。谢谢,
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);
答案 0 :(得分:0)
我会检查保存excel文件的服务器上的文件夹权限。您需要确保您的Web应用程序具有该文件夹的读/写权限。
它在本地计算机上运行,而不是在托管环境中运行,这让我觉得文件夹权限可能是问题所在。