我一直试图将.xls文件转换为.xlsx,而不必依赖Office组件。我关注了c-sharp-convert-xls-file-to-xlsx-without-office-components
它将文件从xls转换为xlsx,但所有格式均丢失。我尝试了其他库,例如Spire.XLS,NPOI等。但是没有一个库可以将.xls精确复制到.xlsx。请帮助我进行格式化。以下是我用于转换的链接中的代码:
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;