当前位置:网站首页>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;
}
}
}
}
边栏推荐
猜你喜欢

【Camera基础(一)】Camera摄像头工作原理及整机架构

ping: www.baidu. Com: unknown name or service

Remove the screen recording reminder (seven cattle cloud demo)

A deep learning model for urban traffic flow prediction with traffic events mined from twitter
![在每个树行中找最大值[分层遍历之一的扩展]](/img/5b/81ff20b61c0719ceb6873e44878859.png)
在每个树行中找最大值[分层遍历之一的扩展]

Pattern recognition - 9 Decision tree

即构「畅直播」上线!提供全链路升级的一站式直播服务

Wireshark packet capturing skills summarized by myself

虚拟机CentOS7中无图形界面安装Oracle(保姆级安装)

字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?
随机推荐
Prompt that the device has no permission when using ADB to connect to the device
【吴恩达笔记】卷积神经网络
Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance
TypeScript快速入门
[theory] deep learning in the covid-19 epic: a deep model for urban traffic revitalization index
AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
Memcached comprehensive analysis – 2 Understand memcached memory storage
Intelligent fish tank control system based on STM32 under Internet of things
栈的两种实现方式
Implementation of adjacency table storage array of graph
01---两列波在相遇处发生干涉的条件
About transform InverseTransformPoint, transform. InverseTransofrmDirection
Sslhandshakeexception: no subject alternative names present - sslhandshakeexception: no subject alternative names present
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
最大流问题
LeetCode-513. Find the value in the lower left corner of the tree
VSCode无网环境快速迁移开发环境(VIP典藏版)
VirtualBox virtual machine installation win10 Enterprise Edition
Memcached full profiling – 1 Fundamentals of memcached
Getting started with typescript