当前位置:网站首页>Excel布局
Excel布局
2022-06-24 19:29:00 【xiexuzhao】
using System;
using System.Collections.Generic;
using System.Text;
//using NPOI;
//using NPOI.OOXML;
//using NPOI.OpenXml4Net;
//using NPOI.OpenXmlFormats;
using System.Data;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.SS.Util;
namespace WindowsFormsApplication1
{
public class ExcelHelper : IDisposable
{
private string fileName = null; //文件名
private IWorkbook workbook = null;
private FileStream fs = null;
private bool disposed;
public ExcelHelper(string fileName)
{
this.fileName = fileName;
disposed = false;
}
/// <summary>
/// 将excel中的数据导入到DataTable中
/// </summary>
/// <param name="sheetName">excel工作薄sheet的名称</param>
/// <returns>返回的DataTable</returns>
public string ExcelToDataTable(string sheetName)
{
ISheet sheet = null;
int startRow = 0;
StringBuilder stringBuilder = new StringBuilder();
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
if (fileName.IndexOf(".xlsx") > 0) // 2007版本
workbook = new XSSFWorkbook(fs);
else if (fileName.IndexOf(".xls") > 0) // 2003版本
workbook = new HSSFWorkbook(fs);
if (sheetName != null)
{
sheet = workbook.GetSheet(sheetName);
if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
{
sheet = workbook.GetSheetAt(0);
}
}
else
{
sheet = workbook.GetSheetAt(0);
}
if (sheet != null)
{
IRow firstRow = sheet.GetRow(0);
int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数
//最后一列的标号
int rowCount = sheet.LastRowNum;
stringBuilder.AppendLine("<table>");
for (int i = startRow; i <= rowCount; ++i)
{
if (i == rowCount)
{
}
IRow row = sheet.GetRow(i);
//int colspanCloseIndex = 0; //开始合并
stringBuilder.AppendLine("<tr>");
if (row == null) continue; //没有数据的行默认是null
//
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
ICell cell = row.GetCell(j);
if (cell != null) //同理,没有数据的单元格都默认是null
{
if (cell.IsMergedCell)
{
if (j== row.FirstCellNum)
{
string tag = string.Empty;
List<CellRangeAddress> MergedRegions = sheet.MergedRegions;
foreach (CellRangeAddress rangeAddress in MergedRegions)
{
//全在同行列的
if (rangeAddress.FirstRow == i && rangeAddress.LastRow == i)
{
tag = "colspan =\"" + cellCount + "\"";
break;
}
//全在同一列的
if (rangeAddress.FirstColumn == j && rangeAddress.LastColumn == j)
{
tag = "rowspan =\"" + cellCount + "\"";
break;
}
}
//colspanCloseIndex = j + cell.RichStringCellValue.Length; //开始合并到结束合并的表格列。
//rowspan = ""
stringBuilder.AppendLine("<td " + tag + ">" + cell.StringCellValue + "</td>");
}
}
else
{
stringBuilder.AppendLine("<td>" + cell.StringCellValue + "</td>");
}
//string cellValue = cell.StringCellValue;
//if (cellValue != null)
//{
// stringBuilder.AppendLine("<td>"+ cellValue + "</td>");
//}
}
}
stringBuilder.AppendLine("</tr>");
}
stringBuilder.AppendLine("</table>");
}
return stringBuilder.ToString();
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
return null;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
if (fs != null)
fs.Close();
}
fs = null;
disposed = true;
}
}
}
}
边栏推荐
- 2022国际女性工程师日:戴森设计大奖彰显女性设计实力
- Docking of arkit and character creator animation curves
- Blender's simple skills - array, rotation, array and curve
- 煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导
- 队列实现原理和应用
- socket done
- 基于 KubeSphere 的分级管理实践
- VirtualBox virtual machine installation win10 Enterprise Edition
- 降低pip到指定版本(通过PyCharm升级pip,在降低到原来版本)
- 力扣每日一题-第26天-496.下一个更大元素Ⅰ
猜你喜欢

About transform InverseTransformPoint, transform. InverseTransofrmDirection

Li Kou daily question - day 26 -496 Next larger element I

A deep learning model for urban traffic flow prediction with traffic events mined from twitter

我国SaaS产业的发展趋势与路径

多路转接select

【吴恩达笔记】机器学习基础

2022国际女性工程师日:戴森设计大奖彰显女性设计实力

LeetCode-513. 找树左下角的值

Datakit 代理实现局域网数据统一汇聚

66 pitfalls in go programming language: pitfalls and common errors of golang developers
随机推荐
C语言-关键字1
A field in the database is of JSON type and stores ["1", "2", "3"]
socket(1)
面试官:你说你精通Redis,你看过持久化的配置吗?
Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
The collection of zero code enterprise application cases in various industries was officially released
Web project deployment
TDengine可通过数据同步工具 DataX读写
传输层 udp && tcp
[精选] 多账号统一登录,你如何设计?
虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)
Antdb database online training has started! More flexible, professional and rich
Implementing DNS requester with C language
Slider控制Animator动画播放进度
socket(2)
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
我国SaaS产业的发展趋势与路径
Memcached comprehensive analysis – 2 Understand memcached memory storage
双链表实现
Li Kou daily question - day 25 -496 Next larger element I