使用C#Asp.net Web应用程序将ibfs(从网站下载的Excel文件)文件转换为.xls / .xlsx文件格式

我有一个要求,我需要从不同的资源格式input文件。 我的应用程序通过file upload接收input(xls / xlsx / ibfs )文件,并执行条件检查并以.xlsx文件格式生成格式化的输出。 我所有的input文件都是由不同的在线公共数据报告网站生成的报告。 当我以excel格式下载某些网站正在生产“WFServlet.ibfs”文件格式。

这不是一个重复的职位。 我尝试了不同的方法,并在这里提出了build议,尝试了几种方法,但没有解决我的问题。 对不起,很长的职位。 我希望有前辈build议或帮助解决这个问题。

我能够使用C#OLEDB ACE引擎处理xls和xlsx文件格式。 它在我的本地机器以及我本地托pipe的IIS服务器中工作得非常好。 但是当我上传.ibfs文件格式时,我得到的问题。

示例input

在这里输入图像说明

在这里,我发布了我最有效的两种不同的方法:

方法1(使用Microsoft Office Interop)
方法2(使用第三方EPPlus库)

1.方法1(使用Microsoft Office Interop):

在这种方法中,我使用了Microsoft Interop dll。 最初在转换之前,我将.ibfs的扩展名更改为.xls,然后使用Microsoft Interop将xls转换为xlsx文件格式。 在这种方法,它在我的本地机器工作正常。 但它不能在我的本地IIS服务器上工作(在我的本地IIS服务器中,我可以将扩展名从.ibfs改为.xls,但是之后不会从xls创buildxlsx文件) 。 我添加了Office12“Microsoft.Office.Interop.Excel.dll”和“Office.dll”的DLL到我的项目参考。

但是用这种方法,我将来可能会遇到问题。 目前Office安装在本地机器上,但是当我们将代码移动到服务器时,我们没有安装Office, 客户端也不想在服务器上安装Office。 我不确定它是否会在没有安装Office的同一个DLL的服务器上工作。

以下是代码:

步骤1:如果用户上传的文件是.ibfs文件types,则将.ibs扩展名改为.xls并调用转换方法

string path ="C:\\testinput\\"; string extension = Path.GetExtension(InputFile.FileName); // get the extension of user upload file string fileName = "testFile"+ extension; // make a new name to assign to the user uplaoded file InputFile.SaveAs(path + fileName); // save the user uploaded file into the testinput folder with testFile file name inputFileWithPath = path + fileName; // copy the path of saved file "C:\\testinput\\testFile+extenstion" newPath = inputFileWithPath; // used if input file is of .ibfs or .xls extn if (extension.Equals(".IBFS") || extension.Equals(".ibfs")) { //input user uploaded file extension is .ibfs , If file already exist in the upload folder path then delete the old one before File.Move if (File.Exists(newPath + ".ibfs")) { File.Delete(newPath); } else { newPath = Path.ChangeExtension(inputFileWithPath, ".xls"); // chnage the file extension from .ibfs to .xls File.Move(inputFileWithPath, newPath); // move the new file .xls to testinput path inputFileWithPath = excelComm.convertExel(newPath); // convert the .xls file into .xlsx file format } } 

第2步现在使用Interop将逻辑从.xls转换为xlsx

 public string convertExel(string FilePath) { string path = ""; var app = new Microsoft.Office.Interop.Excel.Application(); try { if (File.Exists(FilePath + "x")) // check if file with .xlsx is already exist, if exist delete it { File.Delete(FilePath + "x"); } else { var wb = app.Workbooks.Open(FilePath); wb.SaveAs(Filename: FilePath + "x", FileFormat: Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook); path = FilePath + "x"; wb.Close(); } } // end of try catch (Exception ex) { string errorMsg = ""; CatchException(ex, errorMsg); } return path; } 

2.方法2(使用第三方EPPlus库):我下载了EPPlus.dll并添加到我的项目参考。 我使用下面的代码。 这基本上改变了.ibfs到xls的扩展名,并调用convertExcel方法,将xls转换成数据集中的数据集,将数据表复制到工作簿表中,并保存为.xlsx文件。 但它不工作。

以下是代码示例

步骤1:如果用户上传的文件是.ibfs文件types,则将扩展名从.ibs更改为.xls并调用转换方法

步骤1与上述方法1相同。

第2步:使用EPPlus库从.xls转换为xlsx。 为此,我遵循C#的解决scheme- 将xls文件转换为xlsx,而不使用office组件

 public string convertExel(string FilePath) { string path = ""; try { if (File.Exists(FilePath + "x"))// check if file with .xlsx is already exist, if exist delete it {File.Delete(FilePath + "x");} else { string fileName = Path.GetFileNameWithoutExtension(FilePath); string filePathXlsx = "C:\\testinput\\"+ fileName + ".xlsx "; using (ExcelPackage epackage = new ExcelPackage()) { ExcelWorksheet excel = epackage.Workbook.Worksheets.Add("Sheet1"); DataSet ds = ReadExcelFile(FilePath); // Causing Error HERE DataTable dtbl = ds.Tables[0]; excel.Cells["A1"].LoadFromDataTable(dtbl, true); System.IO.FileInfo file = new System.IO.FileInfo(filePathXlsx); epackage.SaveAs(file); path = filePathXlsx; } // end of using }// end of else }//end of try catch (Exception ex) { string errorMsg = ""; CatchException(ex, errorMsg); } return path; } // end of method 

//从excel文件生成数据集

  private static DataSet ReadExcelFile(string FilePath) { string constr = ""; DataSet ds = new DataSet(); string extension = Path.GetExtension(FilePath); if (extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase))//Checking for the extentions, if XLS connect using ACE OleDB { constr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FilePath + ";Extended Properties=\"Excel 8.0;IMEX=1;HDR=YES\""; } //Use ACE OleDb if xlsx extention else if (extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase)) { constr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;IMEX=1;HDR=YES\"", FilePath); } else { constr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FilePath + ";Extended Properties=\"Excel 8.0;IMEX=1;HDR=YES\""; } using (OleDbConnection conn = new OleDbConnection(constr)) { conn.Open(); // causing error HERE OleDbCommand cmd = new OleDbCommand(); cmd.Connection = conn; DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); // Get all Sheets in Excel File foreach (DataRow dr in dtSheet.Rows) // Loop through all Sheets to get data { string sheetName = dr["TABLE_NAME"].ToString(); cmd.CommandText = "SELECT * FROM [" + sheetName + "]"; // Get all rows from the Sheet DataTable dt = new DataTable(); dt.TableName = sheetName; OleDbDataAdapter da = new OleDbDataAdapter(cmd); da.Fill(dt); ds.Tables.Add(dt); } // end of for cmd = null; conn.Close(); } // end of using return ds; } 

它给我错误“System.Data.OleDb.OleDbException(0x80004005): 外部表格不是预期的格式。 在System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr,OleDbConnection连接)“

它将扩展名从.ibfs改为xls,但之后不生成任何xlsx文件。 我尝试了不同的连接string,ACE,与Xml格式的引擎,HTML导入,单引号和双引号,但没有任何工作。 下载的网页excel文件是否以任何特定的格式不被OLEDB支持? 我不知道如何处理这种types的特定格式。

我很感激,如果任何人可以给我任何想法,我可以如何解决' ibfs '文件格式的问题。

我的最新更新:我尝试了Spire.XLS,但没有与'.ibfs'文件格式。 它只是在xls和xlsx格式下正常工作。

只有一个请求,请只build议开源dll的。 我不能在客户机(服务器)上安装任何软件。 我只能select使用EPPlus之类的开放源代码库或没有任何安装的只有dll支持的任何东西。 谢谢。

使用Extended Properties=\"Excel 12.0 Xml;IMEX=1;HDR=YES\"尝试replaceExtended Properties=\"Excel 12.0;IMEX=1;HDR=YES\"

 //Use ACE OleDb if xlsx extention else if (extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase)) { constr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0;IMEX=1;HDR=YES\"", FilePath); } 

如果IBFS文件是Excel文件,您可以尝试使用Essential XlsIO 。 我无法find任何IBFS文件来检查。

如果您符合条件,整套控件都可以通过社区许可证程序免费获得(商业应用程序)。 社区许可证是完整的产品,没有限制或水印。

注意:我为Syncfusion工作。