背景:MinExcel开源类库,导数据的库,占用内存很低,通过io,不通过内存保存,不支持 xls格式的文件,支持csv和xlsx,所以要想使用这个库,就得把xls格式转换为xlsx。只复制了数据 合并单元格,没复制格式这些。
public string ConvertToXlsx(string xlsPath, string newExcelPath)
{
var oldWorkbook = new HSSFWorkbook(new FileStream(xlsPath, FileMode.Open));
var oldWorkSheet = oldWorkbook.GetSheetAt(0);
int m = 0; try
{ using (var fileStream = new FileStream(newExcelPath, FileMode.Create))
{
var newWorkBook = new XSSFWorkbook();
var newWorkSheet = newWorkBook.CreateSheet("Sheet1");
int i = 0;
foreach (HSSFRow oldRow in oldWorkSheet)
{ var newRow = newWorkSheet.CreateRow(oldRow.RowNum); for (int ii = oldRow.FirstCellNum; ii < oldRow.Cells.Count; ii++)
{
m = ii;
var newCell = newRow.CreateCell(ii);
newCell.SetCellValue(GetValueType(oldRow.Cells[ii]).ToString());
} } int sheetMergerCount = oldWorkSheet.NumMergedRegions;
for (int me = 0; me < sheetMergerCount; me++)
newWorkSheet.AddMergedRegion(oldWorkSheet.GetMergedRegion(me)); newWorkBook.Write(fileStream);
newWorkBook.Close();
}
}
catch (Exception ex)
{
int b = m;
throw;
}
oldWorkbook.Close(); return newExcelPath;
}
网上找的一个更完善的用法,带格式:

using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FixtureDataImportFromExcel.Common
{
public static class NPOIExt
{
/// <summary>
/// 跨工作薄Workbook复制工作表Sheet
/// </summary>
/// <param name="sSheet">源工作表Sheet</param>
/// <param name="dWb">目标工作薄Workbook</param>
/// <param name="dSheetName">目标工作表Sheet名</param>
/// <param name="clonePrintSetup">是否复制打印设置</param>
public static ISheet CrossCloneSheet(this ISheet sSheet, IWorkbook dWb, string dSheetName, bool clonePrintSetup)
{
ISheet dSheet;
dSheetName = string.IsNullOrEmpty(dSheetName) ? sSheet.SheetName : dSheetName;
dSheetName = (dWb.GetSheet(dSheetName) == null) ? dSheetName : dSheetName + "_拷贝";
dSheet = dWb.GetSheet(dSheetName) ?? dWb.CreateSheet(dSheetName);
CopySheet(sSheet, dSheet);
if (clonePrintSetup)
ClonePrintSetup(sSheet, dSheet);
dWb.SetActiveSheet(dWb.GetSheetIndex(dSheet)); //当前Sheet作为下次打开默认Sheet
return dSheet;
}
/// <summary>
/// 跨工作薄Workbook复制工作表Sheet
/// </summary>
/// <param name="sSheet">源工作表Sheet</param>
/// <param name="dWb">目标工作薄Workbook</param>
/// <param name="dSheetName">目标工作表Sheet名</param>
public static ISheet CrossCloneSheet(this ISheet sSheet, IWorkbook dWb, string dSheetName)
{
bool clonePrintSetup = true;
return CrossCloneSheet(sSheet, dWb, dSheetName, clonePrintSetup);
}
/// <summary>
/// 跨工作薄Workbook复制工作表Sheet
/// </summary>
/// <param name="sSheet">源工作表Sheet</param>
/// <param name="dWb">目标工作薄Workbook</param>
public static ISheet CrossCloneSheet(this ISheet sSheet, IWorkbook dWb)
{
string dSheetName = sSheet.SheetName;
bool clonePrintSetup = true;
return CrossCloneSheet(sSheet, dWb, dSheetName, clonePrintSetup);
} private static IFont FindFont(this IWorkbook dWb, IFont font, List<IFont> dFonts)
{
//IFont dFont = dWb.FindFont(font.Boldweight, font.Color, (short)font.FontHeight, font.FontName, font.IsItalic, font.IsStrikeout, font.TypeOffset, font.Underline);
IFont dFont = null;
foreach (IFont currFont in dFonts)
{
//if (currFont.Charset != font.Charset) continue;
//else
//if (currFont.Color != font.Color) continue;
//else
if (currFont.FontName != font.FontName) continue;
else if (currFont.FontHeight != font.FontHeight) continue;
else if (currFont.IsBold != font.IsBold) continue;
else if (currFont.IsItalic != font.IsItalic) continue;
else if (currFont.IsStrikeout != font.IsStrikeout) continue;
else if (currFont.Underline != font.Underline) continue;
else if (currFont.TypeOffset != font.TypeOffset) continue;
else { dFont = currFont; break; }
}
return dFont;
}
private static ICellStyle FindStyle(this IWorkbook dWb, IWorkbook sWb, ICellStyle style, List<ICellStyle> dCellStyles, List<IFont> dFonts)
{
ICellStyle dStyle = null;
foreach (ICellStyle currStyle in dCellStyles)
{
if (currStyle.Alignment != style.Alignment) continue;
else if (currStyle.VerticalAlignment != style.VerticalAlignment) continue;
else if (currStyle.BorderTop != style.BorderTop) continue;
else if (currStyle.BorderBottom != style.BorderBottom) continue;
else if (currStyle.BorderLeft != style.BorderLeft) continue;
else if (currStyle.BorderRight != style.BorderRight) continue;
else if (currStyle.TopBorderColor != style.TopBorderColor) continue;
else if (currStyle.BottomBorderColor != style.BottomBorderColor) continue;
else if (currStyle.LeftBorderColor != style.LeftBorderColor) continue;
else if (currStyle.RightBorderColor != style.RightBorderColor) continue;
//else if (currStyle.BorderDiagonal != style.BorderDiagonal) continue;
//else if (currStyle.BorderDiagonalColor != style.BorderDiagonalColor) continue;
//else if (currStyle.BorderDiagonalLineStyle != style.BorderDiagonalLineStyle) continue;
//else if (currStyle.FillBackgroundColor != style.FillBackgroundColor) continue;
//else if (currStyle.FillBackgroundColorColor != style.FillBackgroundColorColor) continue;
//else if (currStyle.FillForegroundColor != style.FillForegroundColor) continue;
//else if (currStyle.FillForegroundColorColor != style.FillForegroundColorColor) continue;
//else if (currStyle.FillPattern != style.FillPattern) continue;
else if (currStyle.Indention != style.Indention) continue;
else if (currStyle.IsHidden != style.IsHidden) continue;
else if (currStyle.IsLocked != style.IsLocked) continue;
else if (currStyle.Rotation != style.Rotation) continue;
else if (currStyle.ShrinkToFit != style.ShrinkToFit) continue;
else if (currStyle.WrapText != style.WrapText) continue;
else if (!currStyle.GetDataFormatString().Equals(style.GetDataFormatString())) continue;
else
{
IFont sFont = sWb.GetFontAt(style.FontIndex);
IFont dFont = dWb.FindFont(sFont, dFonts);
if (dFont == null) continue;
else
{
currStyle.SetFont(dFont);
dStyle = currStyle;
break;
}
}
}
return dStyle;
}
private static IFont CopyFont(this IFont dFont, IFont sFont, List<IFont> dFonts)
{
//dFont.Charset = sFont.Charset;
//dFont.Color = sFont.Color;
dFont.FontHeight = sFont.FontHeight;
dFont.FontName = sFont.FontName;
dFont.IsBold = sFont.IsBold;
dFont.IsItalic = sFont.IsItalic;
dFont.IsStrikeout = sFont.IsStrikeout;
dFont.Underline = sFont.Underline;
dFont.TypeOffset = sFont.TypeOffset;
dFonts.Add(dFont);
return dFont;
}
private static ICellStyle CopyStyle(this ICellStyle dCellStyle, ICellStyle sCellStyle, IWorkbook dWb, IWorkbook sWb, List<ICellStyle> dCellStyles, List<IFont> dFonts)
{
ICellStyle currCellStyle = dCellStyle;
currCellStyle.Alignment = sCellStyle.Alignment;
currCellStyle.VerticalAlignment = sCellStyle.VerticalAlignment;
currCellStyle.BorderTop = sCellStyle.BorderTop;
currCellStyle.BorderBottom = sCellStyle.BorderBottom;
currCellStyle.BorderLeft = sCellStyle.BorderLeft;
currCellStyle.BorderRight = sCellStyle.BorderRight;
currCellStyle.TopBorderColor = sCellStyle.TopBorderColor;
currCellStyle.LeftBorderColor = sCellStyle.LeftBorderColor;
currCellStyle.RightBorderColor = sCellStyle.RightBorderColor;
currCellStyle.BottomBorderColor = sCellStyle.BottomBorderColor;
//dCellStyle.BorderDiagonal = sCellStyle.BorderDiagonal;
//dCellStyle.BorderDiagonalColor = sCellStyle.BorderDiagonalColor;
//dCellStyle.BorderDiagonalLineStyle = sCellStyle.BorderDiagonalLineStyle;
//dCellStyle.FillBackgroundColor = sCellStyle.FillBackgroundColor;
dCellStyle.FillForegroundColor = sCellStyle.FillForegroundColor;
//dCellStyle.FillPattern = sCellStyle.FillPattern;
currCellStyle.Indention = sCellStyle.Indention;
currCellStyle.IsHidden = sCellStyle.IsHidden;
currCellStyle.IsLocked = sCellStyle.IsLocked;
currCellStyle.Rotation = sCellStyle.Rotation;
currCellStyle.ShrinkToFit = sCellStyle.ShrinkToFit;
currCellStyle.WrapText = sCellStyle.WrapText;
currCellStyle.DataFormat = dWb.CreateDataFormat().GetFormat(sWb.CreateDataFormat().GetFormat(sCellStyle.DataFormat));
IFont sFont = sCellStyle.GetFont(sWb);
IFont dFont = dWb.FindFont(sFont, dFonts) ?? dWb.CreateFont().CopyFont(sFont, dFonts);
currCellStyle.SetFont(dFont);
dCellStyles.Add(currCellStyle);
return currCellStyle;
} private static void CopySheet(ISheet sSheet, ISheet dSheet)
{
var maxColumnNum = 0;
List<ICellStyle> dCellStyles = new List<ICellStyle>();
List<IFont> dFonts = new List<IFont>();
MergerRegion(sSheet, dSheet);
for (int i = sSheet.FirstRowNum; i <= sSheet.LastRowNum; i++)
{
IRow sRow = sSheet.GetRow(i);
IRow dRow = dSheet.CreateRow(i);
if (sRow != null)
{
CopyRow(sRow, dRow, dCellStyles, dFonts);
if (sRow.LastCellNum > maxColumnNum)
maxColumnNum = sRow.LastCellNum;
}
}
for (int i = 0; i <= maxColumnNum; i++)
dSheet.SetColumnWidth(i, sSheet.GetColumnWidth(i));
}
private static void CopyRow(IRow sRow, IRow dRow, List<ICellStyle> dCellStyles, List<IFont> dFonts)
{
dRow.Height = sRow.Height;
ISheet sSheet = sRow.Sheet;
ISheet dSheet = dRow.Sheet;
for (int j = sRow.FirstCellNum; j <= sRow.LastCellNum; j++)
{
NPOI.SS.UserModel.ICell sCell = sRow.GetCell(j);
NPOI.SS.UserModel.ICell dCell = dRow.GetCell(j);
if (sCell != null)
{
if (dCell == null)
dCell = dRow.CreateCell(j);
CopyCell(sCell, dCell, dCellStyles, dFonts);
}
}
}
private static void CopyCell(NPOI.SS.UserModel.ICell sCell, NPOI.SS.UserModel.ICell dCell, List<ICellStyle> dCellStyles, List<IFont> dFonts)
{
ICellStyle currCellStyle = dCell.Sheet.Workbook.FindStyle(sCell.Sheet.Workbook, sCell.CellStyle, dCellStyles, dFonts);
if (currCellStyle == null)
currCellStyle = dCell.Sheet.Workbook.CreateCellStyle().CopyStyle(sCell.CellStyle, dCell.Sheet.Workbook, sCell.Sheet.Workbook, dCellStyles, dFonts);
dCell.CellStyle = currCellStyle;
switch (sCell.CellType)
{
case CellType.String:
dCell.SetCellValue(sCell.StringCellValue);
break;
case CellType.Numeric:
dCell.SetCellValue(sCell.NumericCellValue);
break;
case CellType.Blank:
dCell.SetCellType(CellType.Blank);
break;
case CellType.Boolean:
dCell.SetCellValue(sCell.BooleanCellValue);
break;
case CellType.Error:
dCell.SetCellValue(sCell.ErrorCellValue);
break;
case CellType.Formula:
dCell.SetCellFormula(sCell.CellFormula);
break;
default:
break;
}
} private static void MergerRegion(ISheet sSheet, ISheet dSheet)
{
int sheetMergerCount = sSheet.NumMergedRegions;
for (int i = 0; i < sheetMergerCount; i++)
dSheet.AddMergedRegion(sSheet.GetMergedRegion(i));
}
private static void ClonePrintSetup(ISheet sSheet, ISheet dSheet)
{
//工作表Sheet页面打印设置
dSheet.PrintSetup.Copies = 1; //打印份数
dSheet.PrintSetup.PaperSize = sSheet.PrintSetup.PaperSize; //纸张大小
dSheet.PrintSetup.Landscape = sSheet.PrintSetup.Landscape; //纸张方向:默认纵向false(横向true)
dSheet.PrintSetup.Scale = sSheet.PrintSetup.Scale; //缩放方式比例
dSheet.PrintSetup.FitHeight = sSheet.PrintSetup.FitHeight; //调整方式页高
dSheet.PrintSetup.FitWidth = sSheet.PrintSetup.FitWidth; //调整方式页宽
dSheet.PrintSetup.FooterMargin = sSheet.PrintSetup.FooterMargin;
dSheet.PrintSetup.HeaderMargin = sSheet.PrintSetup.HeaderMargin;
//页边距
dSheet.SetMargin(MarginType.TopMargin, sSheet.GetMargin(MarginType.TopMargin));
dSheet.SetMargin(MarginType.BottomMargin, sSheet.GetMargin(MarginType.BottomMargin));
dSheet.SetMargin(MarginType.LeftMargin, sSheet.GetMargin(MarginType.LeftMargin));
dSheet.SetMargin(MarginType.RightMargin, sSheet.GetMargin(MarginType.RightMargin));
dSheet.SetMargin(MarginType.HeaderMargin, sSheet.GetMargin(MarginType.HeaderMargin));
dSheet.SetMargin(MarginType.FooterMargin, sSheet.GetMargin(MarginType.FooterMargin));
//页眉页脚
dSheet.Header.Left = sSheet.Header.Left;
dSheet.Header.Center = sSheet.Header.Center;
dSheet.Header.Right = sSheet.Header.Right;
dSheet.Footer.Left = sSheet.Footer.Left;
dSheet.Footer.Center = sSheet.Footer.Center;
dSheet.Footer.Right = sSheet.Footer.Right;
//工作表Sheet参数设置
dSheet.IsPrintGridlines = sSheet.IsPrintGridlines; //true: 打印整表网格线。不单独设置CellStyle时外框实线内框虚线。 false: 自己设置网格线
dSheet.FitToPage = sSheet.FitToPage; //自适应页面
dSheet.HorizontallyCenter = sSheet.HorizontallyCenter; //打印页面为水平居中
dSheet.VerticallyCenter = sSheet.VerticallyCenter; //打印页面为垂直居中
dSheet.RepeatingRows = sSheet.RepeatingRows; //工作表顶端标题行范围
}
}
}

使用:

using (var fileStream = new FileStream(newExcelPath, FileMode.Create))
{
var newWorkBook1 = new XSSFWorkbook();
var sheet = oldWorkSheet.CrossCloneSheet(newWorkBook1, "Sheet1");
newWorkBook1.Add(sheet);
newWorkBook1.Write(fileStream);
newWorkBook1.Close(); }
oldWorkbook.Close();
大部分格式都得行
这个类的原文地址:

C# 借助NPOI 完成 xls 转换为xlsx的更多相关文章

  1. NPOI导入xls,xlsx格式实例

    NPOI DLL下载地:http://npoi.codeplex.com/releases using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; us ...

  2. Python将excel文件从xls转换为xlsx

    本文使用场景:将一个xls格式Excel文件转换为xlsx文件格式.接下来将一步一步演示该操作.你也可以对代码进行修改使其适用于你所需的场景. 安装Python3 首先需要安装Python,我这里安装 ...

  3. 用VB把xls转换为xlsx

    Sub xls批量转换成xlsx()Application.ScreenUpdating = FalseMsgBox "现在开始转换,请稍候!"mypath = ThisWorkb ...

  4. 用NPOI实现导入导出csv、xls、xlsx数据功能

    用NPOI实现导入导出csv.xls.xlsx数据功能   直接上代码 首先定义一个接口   如果需要直接操作文件的话,就自己在封装一次 然后定义csv类的具体实现 这个需要引入命名空间LumenWo ...

  5. C# 将DataGridView中显示的数据导出到Excel(.xls和.xlsx格式)—NPOI

    前言 https://blog.csdn.net/IT_xiao_guang_guang/article/details/104217491  本地数据库表中有46785条数据,测试正常  初次运行程 ...

  6. C#仪器数据文件解析-Excel文件(xls、xlsx)

    不少仪器工作站可以将数据导出为Excel文件,包括97-2003版本的xls文件和2007+的xlsx文件. 采集Excel文件相比采集pdf文件更容易.程序更健壮,毕竟Excel中数据有明确的行.列 ...

  7. Java 解析Excel(xls、xlsx两种格式)

    Java 解析Excel(xls.xlsx两种格式) 一.环境 JDK 1.8 二.JAR 1.commons-collections4-4.1.jar 2.poi-3.9-20121203.jar ...

  8. 1、创建一个空白的xls和xlsx文件

    1.创建一个空白的xls文件 Step1:先引入库NPOI.dll文件 Step2: ①:实例化一个workbook,实为在内存表中创建一个xls文件 NPOI.HSSF.UserModel.HSSF ...

  9. C# 操作 Excel(.xls和.xlsx)文件

    C#创建Excel(.xls和.xlsx)文件的三种方法 .NET 使用NPOI导入导出标准Excel C# 使用NPOI 实现Excel的简单导入导出 NET使用NPOI组件将数据导出Excel-通 ...

  10. Java解析Excel工具类(兼容xls和xlsx)

    依赖jar <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml&l ...

随机推荐

  1. 基于C语言的面向对象设计模式(持续更新)

    前言 首先这篇文章只是初步的尝试,不涉及过于高深的编程技巧:同时需要表明的是,面向对象只是一种思想,不局限于什么样的编程语言,不可否认的是基于面向对象特性而设计的语言确实要比面向过程式的语言更加容易进 ...

  2. 比nestjs更优雅的ioc:跨模块访问资源

    使用ts的最佳境界:化类型于无形 在项目中使用ts可以带来类型智能提示与校验的诸多好处.同时,为了减少类型标注,达到化类型于无形的效果,CabloyJS引入了ioc和依赖查找的机制.在上一篇文章中,我 ...

  3. SpringBoot常用注解整理

    @SpringBootApplication 定义在main方法入口类处,用于启动sping boot应用项目 @Target(ElementType.TYPE) @Retention(Retenti ...

  4. HarmonyOS音视频开发概述

      在音视频开发指导中,将介绍各种涉及音频.视频播放或录制功能场景的开发方式,指导开发者如何使用系统提供的音视频API实现对应功能.比如使用TonePlayer实现简单的提示音,当设备接收到新消息时, ...

  5. 如何将 ASP.NET Core MVC 项目的视图分离到另一个项目

    如何将 ASP.NET Core MVC 项目的视图分离到另一个项目 在当下这个年代 SPA 已是主流,人们早已忘记了 MVC 以及 Razor 的故事.但是在某些场景下 SSR 还是有意想不到效果. ...

  6. HL7传输协议

    HL7消息通过各种TCP/IP传输发送,其中一些包括: 下层协议(LLP) 文件传输协议(FTP) 简单对象访问协议(SOAP) 简单邮件传输协议(SMTP) 尽管HL7可以使用多种传输协议进行数据传 ...

  7. 牛客网-SQL专项训练16

    ①在book表中,将工具书类型(tool)的书的书架序号都减少2,下列语句正确的是(C) 解析: 题目要求的批量更改,insert 是更改数据,排除B,update与set搭配使用,排除选项D,whe ...

  8. 深入解读:获得 2021 Forrester 全球云数仓卓越表现者的阿里云数据仓库

    简介: 阿里云在最新发布的 The Forrester Wave: Cloud Data Warehouse, Q1 2021 全球云数据仓库技术评比中进入卓越表现者象限,成为国内唯一入选厂商.本文针 ...

  9. 云原生 DevOps,模型化应用交付能力很重要!

    ​简介: DevOps 文化及其支撑其落地实践的自动化工具与平台能力在云原生架构渐为普及的背后,发挥了关键的价值. 撰稿:溪洋 云原生正在成为企业业务创新和解决规模化挑战的加速器. 云原生带来的变革绝 ...

  10. 为 RabbitMQ 服务器启用 SSL/TLS

    为 RabbitMQ 服务器启用 SSL/TLS 目录 为 RabbitMQ 服务器启用 SSL/TLS 为客户端和服务器生成自签名证书 在 RabbitMQ 服务器中启用 TLS/SSL 支持 使用 ...