当前位置:网站首页>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;
}
}
}
}
边栏推荐
- C语言-关键字1
- 01---两列波在相遇处发生干涉的条件
- Data link layer & some other protocols or technologies
- Network layer & IP
- Understanding openstack network
- 平衡二叉搜索树
- 2022 international women engineers' Day: Dyson design award shows women's design strength
- 【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
- Remove the screen recording reminder (seven cattle cloud demo)
- Why are life science enterprises on the cloud in succession?
猜你喜欢
随机推荐
WMI and PowerShell get TCP connection list
关于Unity中的transform.InverseTransformPoint, transform.InverseTransofrmDirection
如何做到全彩户外LED显示屏节能环保
力扣每日一题-第25天-496.下一个更大元素Ⅰ
Unity about conversion between local and world coordinates
Getting started with typescript
Memcached comprehensive analysis – 3 Deletion mechanism and development direction of memcached
心楼:华为运动健康的七年筑造之旅
字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?
Web project deployment
Redis+Caffeine两级缓存,让访问速度纵享丝滑
【无标题】
That is to say, "live broadcast" is launched! One stop live broadcast service with full link upgrade
应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案
面试官:你说你精通Redis,你看过持久化的配置吗?
Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
在每个树行中找最大值[分层遍历之一的扩展]
LeetCode-513. Find the value in the lower left corner of the tree
Direct attack on "three summers" production: good harvest news spreads frequently and summer broadcasting is in full swing
![在每个树行中找最大值[分层遍历之一的扩展]](/img/5b/81ff20b61c0719ceb6873e44878859.png)








