当前位置:网站首页>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;

            }

        }

    }


}
 

原网站

版权声明
本文为[xiexuzhao]所创,转载请带上原文链接,感谢
https://blog.csdn.net/xiexuzhao/article/details/125429277