C# 实现xls类型转换为xlsx类型

76 篇文章 1 订阅

Office97-2003 创建的Excel文件后缀为xls, Office 2007 之后的版本创建的Excel文件后缀为 xlsx,在C#中使用EPPLUS模块读取2003版本创建的Excel存在一些问题,但是2007版本后创建的Excel不存在问题,所以需要将xls 版本转换为xlsx版本再操作,就不存在问题。

例如:

存在一个 Sample.xls 文件

转换代码如下:

private const int OLDOFFICEVESION = -4143;

private const int NEWOFFICEVESION = 56;

void ExcelFormatConvert(string oldExcelfile)

{

var app = new Microsoft.Office.Interop.Excel.Application { Visible = false };

var book = app.Workbooks.Open(oldExcelfile);

int FormatNum; //保存excel文件的格式

string Version; //excel版本号

Version = app.Version;  //获取你使用的excel 的版本号

if (Convert.ToDouble(Version) < 12)//使用Excel 97-2003

{

FormatNum = OLDOFFICEVESION;

}

else

{

FormatNum = NEWOFFICEVESION;  //使用 excel 2007或更新

}

//注意,新的文件名没后缀

//book.SaveAs(Filename: @“newExcel”, FileFormat: Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook);

//book.SaveAs(@“D:\CodeTest\Excel操作\测试报告\NewSample”, FormatNum);

book.SaveAs(@“D:\CodeTest\Excel操作\测试报告\NewSample”, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook);

book.Close();

app.Quit();

}

将老版本格式的xls文件传入到转换函数,将转换后的Excel而文件另存到指定目录下,D:\CodeTest\Excel操作\测试报告\NewSample,且名称为NewSample,无需指定文件后缀的。

在WPF中创建一个Button,执行click事件中执行选择xls文件,转换。

private void btnXls2Xlsx_Click(object sender, RoutedEventArgs e)

{

string filePath = string.Empty;

string fileExt = string.Empty;

OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file

if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) //if there is a file choosen by the user

{

filePath = file.FileName; //get the path of the file

fileExt = System.IO.Path.GetExtension(filePath); //get the file extension

if (fileExt.CompareTo(".xls") == 0) //2003

{

try

{

ExcelFormatConvert(filePath);

}

catch (Exception ex)

{

ShowMessage(“转换失败!\n” + ex.Message);

return;

}

}

}

ShowMessage(“转换成功!”);

}

转换成功后:

文件的后缀变成了 xlsx, 使用Epplus读写不再会存在null的情况了。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用C#中的NPOI库来将Excel数据换为DataSource。下面是一个示例代码: ```csharp using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.Data; public static class ExcelUtility { public static DataTable ExcelToDataTable(string filePath) { IWorkbook workbook = null; ISheet sheet = null; DataTable data = new DataTable(); using (var file = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { if (filePath.EndsWith(".xls")) { workbook = new HSSFWorkbook(file); } else if (filePath.EndsWith(".xlsx")) { workbook = new XSSFWorkbook(file); } if (workbook != null) { sheet = workbook.GetSheetAt(0); if (sheet != null) { var firstRow = sheet.GetRow(0); int cellCount = firstRow.LastCellNum; for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { var cell = firstRow.GetCell(i); if (cell != null) { string columnName = cell.ToString(); if (!string.IsNullOrEmpty(columnName)) { data.Columns.Add(columnName); } } } for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; ++i) { var row = sheet.GetRow(i); if (row != null) { bool emptyRow = true; DataRow dataRow = data.NewRow(); for (int j = row.FirstCellNum; j < cellCount; ++j) { if (row.GetCell(j) != null) { dataRow[j] = row.GetCell(j).ToString(); if (!string.IsNullOrEmpty(dataRow[j].ToString())) { emptyRow = false; } } } if (!emptyRow) { data.Rows.Add(dataRow); } } } } } } return data; } } ``` 这段代码会根据文件路径读取Excel文件,将第一个工作表换为一个DataTable对象,并返回该对象。你可以使用该DataTable对象作为DataSource来绑定数据控件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值