C# 打开下载的excel文件后出现文件格式错误
在我的应用程序中,我试图从mvc中的字节数组内容下载excel文件。文件下载后,当我打开下载的文件时,我得到了一个错误 “您试图打开的文件'XXXX.xls'的格式不同 而不是由文件扩展名指定的。请验证该文件是否为空 在打开文件之前已损坏并且来自受信任的源。是否继续 现在要打开文件吗?” 在上面的错误中单击yes之后,我得到另一个错误 Excel在“XXXX.xls”中发现不可读的内容。你想去吗 是否恢复此工作簿的内容?如果你相信这个消息的来源 请单击“是” 当我再次在上面的错误消息中单击yes时,我再次收到第一条错误消息 “您试图打开的文件'XXXX.xls'的格式不同 而不是由文件扩展名指定的。请验证该文件是否为空 在打开文件之前已损坏并且来自受信任的源。是否继续 现在要打开文件吗?” 在上述错误消息中单击“是”后,excel将打开一个修复弹出窗口,其中显示消息。信息是 修复的记录:格式来自/xl/styles.xml部分(样式) 这是我的控制器代码C# 打开下载的excel文件后出现文件格式错误,c#,asp.net-mvc,excel,C#,Asp.net Mvc,Excel,在我的应用程序中,我试图从mvc中的字节数组内容下载excel文件。文件下载后,当我打开下载的文件时,我得到了一个错误 “您试图打开的文件'XXXX.xls'的格式不同 而不是由文件扩展名指定的。请验证该文件是否为空 在打开文件之前已损坏并且来自受信任的源。是否继续 现在要打开文件吗?” 在上面的错误中单击yes之后,我得到另一个错误 Excel在“XXXX.xls”中发现不可读的内容。你想去吗 是否恢复此工作簿的内容?如果你相信这个消息的来源 请单击“是” 当我再次在上面的错误消息中单击yes
[HttpPost, FileDownload]
public FileContentResult GetReport(DateTime StartDate, DateTime EndDate, int ReportType)
{
var reportData = new Model().GetReport(StartDate, EndDate, ReportType);
string fileName = "Report " + (TimeZoneUtil.ConvertUtcDateTimeToESTDateTime(DateTime.UtcNow).ToString("yyyy:MM:dd:hh:mm:ss")) + ".xls";
return File(reportData, MimeMapping.GetMimeMapping(fileName), fileName);
}
我在视图中使用jQuery文件下载插件调用这个方法,我的代码是
var dataToSend = { "StartDate": $("#dtpreportstartdate").val(), "EndDate": $("#dtpreportenddate").val(), "ReportType": value };
$.fileDownload(GetBaseUrl() + "Dashboard/GetReport",
{
preparingMessageHtml: "success message",
failMessageHtml: "Error message",
httpMethod: "POST",
data: dataToSend
});
下面是我获取excel内容的方法
public byte[] CreateReportFile(List<BGClass> BGRows)
{
MemoryStream ms = new MemoryStream();
SpreadsheetDocument xl = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook);
WorkbookPart wbp = xl.AddWorkbookPart();
WorksheetPart wsp = wbp.AddNewPart<WorksheetPart>();
Workbook wb = new Workbook();
FileVersion fv = new FileVersion();
fv.ApplicationName = "Microsoft Office Excel";
Worksheet ws = new Worksheet();
SheetData sd = new SheetData();
AddStyleSheet(ref xl);
Row headerRow = new Row();
Cell CreatedDateHeaderCell = new Cell() { StyleIndex = Convert.ToUInt32(1) };
CreatedDateHeaderCell.DataType = CellValues.String;
CreatedDateHeaderCell.CellValue = new CellValue("Created Date");
headerRow.Append(CreatedDateHeaderCell);
Cell BackgroundNameHeaderCell = new Cell() { StyleIndex = Convert.ToUInt32(1) };
BackgroundNameHeaderCell.DataType = CellValues.String;
BackgroundNameHeaderCell.CellValue = new CellValue("Bg Name");
headerRow.Append(BackgroundNameHeaderCell);
sd.Append(headerRow);
foreach (BGClass reportRow in BGRows)
{
Row dataRow = new Row();
Cell CreatedDateDataCell = new Cell();
CreatedDateDataCell.DataType = CellValues.String;
CreatedDateDataCell.CellValue = new CellValue(TimeZoneHelper.ConvertUtcDateTimeToESTDateTime(reportRow.CreatedDate).ToString());
dataRow.Append(CreatedDateDataCell);
Cell BackgroundNameDataCell = new Cell();
BackgroundNameDataCell.DataType = CellValues.String;
BackgroundNameDataCell.CellValue = new CellValue(reportRow.BackgroundName);
dataRow.Append(BackgroundNameDataCell);
}
ws.Append(sd);
wsp.Worksheet = ws;
wsp.Worksheet.Save();
Sheets sheets = new Sheets();
Sheet sheet = new Sheet();
sheet.Name = "Report";
sheet.SheetId = 1;
sheet.Id = wbp.GetIdOfPart(wsp);
sheets.Append(sheet);
wb.Append(fv);
wb.Append(sheets);
xl.WorkbookPart.Workbook = wb;
xl.WorkbookPart.Workbook.Save();
xl.Close();
return ms.ToArray();
}
public byte[]CreateReportFile(列出行)
{
MemoryStream ms=新的MemoryStream();
SpreadsheetDocument xl=SpreadsheetDocument.Create(ms,SpreadsheetDocumentType.工作簿);
WorkbookPart wbp=xl.AddWorkbookPart();
工作表部件wsp=wbp.AddNewPart();
工作簿wb=新工作簿();
FileVersion fv=新的FileVersion();
fv.ApplicationName=“Microsoft Office Excel”;
工作表ws=新工作表();
SheetData sd=新的SheetData();
添加样式表(参考xl);
Row headerRow=新行();
Cell createdDataHeaderCell=new Cell(){StyleIndex=Convert.ToUInt32(1)};
CreatedDataHeaderCell.DataType=CellValues.String;
CreatedDataHeaderCell.CellValue=新的CellValue(“创建日期”);
headerRow.Append(createdDataHeaderCell);
Cell BackgroundNameHeaderCell=newcell(){StyleIndex=Convert.ToUInt32(1)};
BackgroundNameHeaderCell.DataType=CellValues.String;
BackgroundNameHeaderCell.CellValue=新的CellValue(“Bg名称”);
headerRow.Append(backgroundnamebeadercell);
sd.追加(headerRow);
foreach(BGRows中的BGClass reportRow)
{
行数据行=新行();
Cell CreatedDataCell=新单元格();
CreatedDataCell.DataType=CellValues.String;
CreatedDataCell.CellValue=新的CellValue(TimeZoneHelper.ConvertUtcDateTimeToESTDateTime(reportRow.CreatedDate.ToString());
追加(CreatedDataCell);
Cell BackgroundNameDataCell=新单元格();
BackgroundNameDataCell.DataType=CellValues.String;
BackgroundNameDataCell.CellValue=新的CellValue(reportRow.BackgroundName);
追加(BackgroundNameDataCell);
}
ws.Append(sd);
wsp.Worksheet=ws;
wsp.Worksheet.Save();
板材=新板材();
板材=新板材();
sheet.Name=“报告”;
sheet.SheetId=1;
sheet.Id=wbp.GetIdOfPart(wsp);
附页(页);
wb.Append(fv);
wb.追加(张);
xl.WorkbookPart.Workbook=wb;
xl.WorkbookPart.Workbook.Save();
xl.Close();
返回ToArray女士();
}
代码有什么问题?为什么打开文件时出现excel错误?
我尝试了很多博客来改变MIME的类型,但是没有任何效果。
有什么想法吗?你在用
这表示您正在编写一个XLSX文件,但您为该文件提供了一个XLS扩展名和相应的MIME类型
将文件扩展名更改为.xlsx
,表示您正在使用的XML格式。
这表示您正在编写一个XLSX文件,但您为该文件提供了一个XLS扩展名和相应的MIME类型
将文件扩展名更改为
.xlsx
,表示XML格式。我可以提出一些建议吗
为什么不使用一个免费的C#库,比如我的(下面的链接),你可以将你的列表
变量传递给它,它会为你创建一个完美工作的.xlsx文件
一行代码,这个问题就消失了:
public void CreateReportFile(List<BGClass> BGRows)
{
CreateExcelFile.CreateExcelDocument(BGRows, "SomeFilename.xlsx");
}
public void CreateReportFile(列出行)
{
CreateExcelFile.CreateExcelDocument(BGRows,“SomeFilename.xlsx”);
}
所有C#源代码都是免费提供的。我能推荐一些吗 为什么不使用一个免费的C#库,比如我的(下面的链接),你可以将你的
列表
变量传递给它,它会为你创建一个完美工作的.xlsx文件
一行代码,这个问题就消失了:
public void CreateReportFile(List<BGClass> BGRows)
{
CreateExcelFile.CreateExcelDocument(BGRows, "SomeFilename.xlsx");
}
public void CreateReportFile(列出行)
{
CreateExcelFile.CreateExcelDocument(BGRows,“SomeFilename.xlsx”);
}
所有C#源代码都是免费提供的。我做了一些研究后解决了这个问题。我使用函数
AddStyleSheet(ref-xl)将样式应用于excel中的标题行代码>。在将样式应用于单元格时,该方法中出现了缺少几个参数的问题
我的老方法是
private WorkbookStylesPart AddStyleSheet(ref SpreadsheetDocument spreadsheet)
{
WorkbookStylesPart stylesheet = spreadsheet.WorkbookPart.AddNewPart<WorkbookStylesPart>();
Stylesheet workbookstylesheet = new Stylesheet();
Font fontBold = new Font(new FontName() { Val = "Arial" }); // Default font
Font defaultFont = new Font(new FontName() { Val = "Arial" }); // Bold font
Bold bold = new Bold();
defaultFont.Append(bold);
Fonts fonts = new Fonts(); // <APENDING Fonts>
fonts.Append(fontBold);
fonts.Append(defaultFont);
//// <Fills>
//Fill fill0 = new Fill(); // Default fill
//Fills fills = new Fills(); // <APENDING Fills>
//fills.Append(fill0);
// <Borders>
//Border border0 = new Border(); // Defualt border
//Borders borders = new Borders(); // <APENDING Borders>
//borders.Append(border0);
// <CellFormats>
CellFormat cellformat0 = new CellFormat() { FontId = 0, FillId = 0, BorderId = 0 }; // Default style : Mandatory | Style ID =0
CellFormat cellformat1 = new CellFormat() { FontId = 1 }; // Style with Bold text ; Style ID = 1
// <APENDING CellFormats>
CellFormats cellformats = new CellFormats();
cellformats.Append(cellformat0);
cellformats.Append(cellformat1);
// Append FONTS, FILLS , BORDERS & CellFormats to stylesheet <Preserve the ORDER>
workbookstylesheet.Append(fonts);
//workbookstylesheet.Append(fills);
//workbookstylesheet.Append(borders);
workbookstylesheet.Append(cellformats);
// Finalize
stylesheet.Stylesheet = workbookstylesheet;
stylesheet.Stylesheet.Save();
return stylesheet;
}
private workbookstypespart AddStyleSheet(参考电子表格文档电子表格)
{
工作台