I have been trying to convert .xls file to .xlsx without having dependecy over Office Component. I followed c-sharp-convert-xls-file-to-xlsx-without-office-components
It converts the file from xls to xlsx but all the formatting gets lost. I have tried other libraries such as Spire.XLS, NPOI etc. But none can do the exact copy of .xls to .xlsx. Please help me with the formatting thing. Below is the code from the link I used for conversion:
XLWorkbook workbook = new XLWorkbook();
DataSet ds = new DataSet();
string connectionString = GetConnectionString();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
// Get all Sheets in Excel File
DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
// Loop through all Sheets to get data
foreach (DataRow dr in dtSheet.Rows)
{
string sheetName = dr["TABLE_NAME"].ToString();
if (!sheetName.EndsWith("$"))
continue;
// Get all rows from the Sheet
cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
DataTable dt = new DataTable();
dt.TableName = sheetName;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(dt);
ds.Tables.Add(dt);
}
cmd = null;
conn.Close();
}
workbook.AddWorksheet(ds);
return ds;