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);
}
}