下文是把xls转成xlsx
try
{
var dlgOpen = new OpenFileDialog
{
Title = @"导入Excel",
Filter = @"所有文件|*.*|Excel文件(*.xls)|*.xls|Excel2007|*.xlsx|CSV文件|*.csv"
};
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
#region 把Excel导入表格中
using (FileStream fs = File.OpenRead(dlgOpen.FileName))
{
var extension = Path.GetExtension(dlgOpen.FileName);
if (extension != null)
{
#region Excel2003,利用GemBox库转成Excel2007
long len = fs.Length;
byte[] buffer = new byte[len];
fs.Read(buffer, 0, (int)len);
fs.Close();
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
var ef = ExcelFile.Load(dlgOpen.FileName);
MemoryStream fsNew = new MemoryStream(buffer, true);
ef.Save(fsNew, new XlsxSaveOptions());
c_reoGridControl.Load(fsNew, FileFormat.Excel2007);
#endregion
}
}
#endregion
SetColumnStyle();
}
}
catch (Exception ex)
{
_errMsg = ex.ToString();
MsgBox.Error(_errMsg);
}
GemBox同样可以用NuGet获取到,不过这是一个付费的dll, 但可以使用它的免费功能,在格式转换上面是没有什么限制的,免费使用注意下面这行
SpreadsheetInfo.SetLicense(“FREE-LIMITED-KEY”);