2

My C# application need to insert data from an Excel file. I'm using the Epplus library to read the Excel file. The problem is that Epplus does not work with an Excel 2003 XLS file, so I need to convert XLS format to XLSX format.

To convert XLS to XLSX, I have to save the XLS to a harddisk. Then I have to read XLS file and save a new XLSX file so that I have 2 files on my harddisk.

Is there anyway that I can convert a file in the XLS format to the XLSX format without saving the physical fle?

Here is my code:

if (extention == ".xls")
{
  string serverPath = HttpContext.Current.Server.MapPath("~/TempData");

  if (!Directory.Exists(serverPath))
  {
      Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~")+ "\\Tempdata");
  }

  string fn = serverPath + "\\" + file.FileName;
  file.SaveAs(fn);

  string fileName = file.FileName.Replace(extention, "");
  string fullName = serverPath +"\\"+ fileName +  ".xlsx";

  var app = new Microsoft.Office.Interop.Excel.Application();
  app.Visible = false;
  try
  {
     var wb = app.Workbooks.Open(fn);
     wb.SaveAs(Filename: fullName , FileFormat: Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook);
     wb.Close(0);
     app.Quit();

     xlsStream = new FileStream(fullName, FileMode.Open);

   }
   catch (Exception ex)
   {
     app.Quit();
     _logger.InsertLog(LogLevel.Error, ex.Message, ex.Message);
   }
 }
Jonathan
  • 6,507
  • 5
  • 37
  • 47
Survivor
  • 59
  • 7
  • Your question makes no sense. Epplus requires a XLSX file; your data is already on disk as an XLS file therefore 2 files are required. Conversion requires a source and a target or am I missing something? –  May 17 '15 at 04:07
  • my application is web application allow user to insert data by excel file. when user click submit button i can receive the file stream. var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null; but if the file is xls i have to convert it to xlsx by saving the tempt file to hard disk . my question is can i convert the xls format to xlsx format without creating a tempt file. – Survivor May 17 '15 at 05:37
  • This may help with the first step _[Read excel file from a stream](http://stackoverflow.com/questions/560435/read-excel-file-from-a-stream)_ –  May 17 '15 at 05:44

0 Answers0