当前位置:网站首页>Excel layout
Excel layout
2022-06-24 21:54: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; // file name
private IWorkbook workbook = null;
private FileStream fs = null;
private bool disposed;
public ExcelHelper(string fileName)
{
this.fileName = fileName;
disposed = false;
}
/// <summary>
/// take excel Import data from to DataTable in
/// </summary>
/// <param name="sheetName">excel Workbooks sheet The name of </param>
/// <returns> Back to 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 edition
workbook = new XSSFWorkbook(fs);
else if (fileName.IndexOf(".xls") > 0) // 2003 edition
workbook = new HSSFWorkbook(fs);
if (sheetName != null)
{
sheet = workbook.GetSheet(sheetName);
if (sheet == null) // If the specified sheetName Corresponding sheet, Try to get the first sheet
{
sheet = workbook.GetSheetAt(0);
}
}
else
{
sheet = workbook.GetSheetAt(0);
}
if (sheet != null)
{
IRow firstRow = sheet.GetRow(0);
int cellCount = firstRow.LastCellNum; // The last one in the line cell The number of That is, the total number of columns
// The label of the last column
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; // Began to merge
stringBuilder.AppendLine("<tr>");
if (row == null) continue; // Lines without data default to null
//
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
ICell cell = row.GetCell(j);
if (cell != null) // Empathy , All cells without data default to null
{
if (cell.IsMergedCell)
{
if (j== row.FirstCellNum)
{
string tag = string.Empty;
List<CellRangeAddress> MergedRegions = sheet.MergedRegions;
foreach (CellRangeAddress rangeAddress in MergedRegions)
{
// All in the same row
if (rangeAddress.FirstRow == i && rangeAddress.LastRow == i)
{
tag = "colspan =\"" + cellCount + "\"";
break;
}
// All in the same column
if (rangeAddress.FirstColumn == j && rangeAddress.LastColumn == j)
{
tag = "rowspan =\"" + cellCount + "\"";
break;
}
}
//colspanCloseIndex = j + cell.RichStringCellValue.Length; // Start merge to end merge table columns .
//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;
}
}
}
}
边栏推荐
- 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
- 福建省发改委福州市营商办莅临育润大健康事业部指导视察工作
- Byte software testing basin friends, you can change jobs. Is this still the byte you are thinking about?
- 字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?
- Introduce the overall process of bootloader, PM, kernel and system startup
- 心楼:华为运动健康的七年筑造之旅
- Datakit 代理实现局域网数据统一汇聚
- 栈的两种实现方式
- Multiplexer select
- LeetCode-513. 找树左下角的值
猜你喜欢

socket done
![[featured] how do you design unified login with multiple accounts?](/img/df/9b4fc11a6971ebe8162ae84250a782.png)
[featured] how do you design unified login with multiple accounts?

XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
![[notes of Wu Enda] multivariable linear regression](/img/b1/60a702aaca58b0afa57ac2f552dabf.png)
[notes of Wu Enda] multivariable linear regression

LeetCode-513. Find the value in the lower left corner of the tree

福建省发改委福州市营商办莅临育润大健康事业部指导视察工作

188. the best time to buy and sell stocks IV

Bld3 getting started UI

将二维数组方阵顺时针旋转90°

Introduce the overall process of bootloader, PM, kernel and system startup
随机推荐
煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导
机器学习:梯度下降法
建木持续集成平台v2.5.0发布
《各行业零代码企业应用案例集锦》正式发布
【吴恩达笔记】机器学习基础
Li Kou daily question - day 25 -496 Next larger element I
Pattern recognition - 9 Decision tree
Application practice | massive data, second level analysis! Flink+doris build a real-time data warehouse scheme
【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
Sslhandshakeexception: no subject alternative names present - sslhandshakeexception: no subject alternative names present
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
将二维数组方阵顺时针旋转90°
平衡二叉搜索树
Slider controls the playback progress of animator animation
[notes of Wu Enda] multivariable linear regression
Redis+Caffeine两级缓存,让访问速度纵享丝滑
Network layer & IP
Datakit agent realizes unified data aggregation in LAN
队列实现原理和应用
旅行商问题(TSP)的相关论文总结