如何使用NPOI c#将xls文件另存为xlsx文件? [英] how to save xls file as xlsx file using NPOI c#?

查看:962
本文介绍了如何使用NPOI c#将xls文件另存为xlsx文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NPOI打开 XLS 文件,然后对XLS文件进行一些修改. 最后,我想将其另存为 XLSX 文件.

I'm using NPOI to open XLS file, then add some modifications to the XLS file. at the end i want to save it as XLSX file.

我正在使用以下代码将其另存为XLS文件:

i'm using this code to save it as XLS file:

using (var fs = new FileStream(Name, FileMode.Create, FileAccess.Write))
{
     wb.Write(fs);
} 

是否可以使用 C#中的 NPOI 将此 XLS文件另存为XLSX文件?

Is it possible to save this XLS file as XLSX file using NPOI in C#?

提前感谢您的回复

推荐答案

一般来说,这是可能的,但并不容易,而是一项非常复杂的任务.

It is possible in general, but not quite easy, rather it is significantly complicated task.

XLS文件格式由HSSFWorkbook类处理(并根据HSSFSheet等).
XLSX文件格式由XSSFWorkbook类(以及XSSFSheet等)处理.

XLS file format is handled by HSSFWorkbook class (and according HSSFSheet and so on).
XLSX file format is handled by XSSFWorkbook class (and XSSFSheet and so on).

因此,为了在使用NPOI打开并修改文件后将文件另存为XLSX,您需要创建新的XSSFWorkbook,然后对于源文件的每个工作表,您都需要创建相应的XSSFSheet,然后将数据复制到从原始工作表中获取数据,依此类推,直到获得完整的数据副本,然后将工作簿保存到文件中.

So in order to save your file as XLSX after you opened and modified it using NPOI, you need to create new XSSFWorkbook, then for each worksheet of your source file you need to create appropriate XSSFSheet, copy data to it from your original worksheet and so on until you will get full copy of your data, and then save workbook to file.

这篇关于如何使用NPOI c#将xls文件另存为xlsx文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆