zoukankan      html  css  js  c++  java
  • NPOI开发手记


    NPOI其实就是POI的.NET移植
    项目地址:http://npoi.codeplex.com/
    git地址:https://github.com/tonyqus/npoi
    教程有些地方不是很完善,不懂的地方可以参考POI,或者直接读取源码研究。

    注意事项

    • 如果你要编辑的行和单元格,原本没有值,或者从未创建过的,就必须先创建。
    • 需要使用的行才创建,需要使用的单元格才创建,否则会占用无用存储空间。
    • 强制重新计算值,如果不设置的话,excel的公式不起作用,除非自己手动回车一次,SetForceFormulaRecalculation(true);

    读取Excel

    using (FileStream fs = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        if (fileName.IndexOf(".xlsx") > 0) // 2007以上版本
            workbook = new XSSFWorkbook(fs);
        else if (fileName.IndexOf(".xls") > 0) // 2003以下版本
            workbook = new HSSFWorkbook(fs);
    }
    

    创建Excel表

    workbook.CreateSheet(sheetName);

    保存Excel

    FileStream fs = File.Create(@"c: emp" + filename);
    //注意,write会自动关闭流。
    xwk.Write(fs);
    //此后面fs就已经关闭释放了。

    创建行
    sheet.CreateRow(index);
    获取行
    sheet.GetRow(index);

    创建列
    row.CreateCell(index);
    设置值
    cell.SetCellValue(value);

    单元格样式

    背景色
    style.FillPattern = FillPatternType.SOLID_FOREGROUND;
    style.FillForegroundColor = HSSFColor.GREY_25_PERCENT.index;

    隐藏单元行
    NPOI.XSSF.UserModel.XSSFRow r = sheet.GetRow(i) as NPOI.XSSF.UserModel.XSSFRow;
    //如果没有值,隐藏次行
    if (r.GetCell(0).CellType != CellType.Numeric && r.GetCell(0).StringCellValue == string.Empty)
    {
    r.ZeroHeight = true;
    }

    水平对齐居中
    style.Alignment = HorizontalAlignment.Center;
    垂直居中
    style.VerticalAlignment = VerticalAlignment.Center;

    字体样式
    //新建一个字体样式对象
    IFont font = workbook.CreateFont();
    //设置字体加粗样式
    font.Boldweight = short.MaxValue;
    //使用SetFont方法将字体样式添加到单元格样式中
    style.SetFont(font);
    //将新的样式赋给单元格
    cell.CellStyle = style;

    设置单元格有边框
    ICellStyle cs = xwk.CreateCellStyle();
    cs.BorderRight = BorderStyle.Thin;
    cs.BorderLeft = BorderStyle.Thin;
    cs.BorderTop = BorderStyle.Thin;
    cs.BorderBottom = BorderStyle.Thin;
    cell.CellStyle = cs;

    设置单元格宽高

    设置单元格的高度实际是设置其所在行高,所以要在单元格所在行上设置行高,行高设置数值好像是像素点的1/20,所以20以便达到设置效果;
    设置单元格的宽度实际上是设置其所在列宽,所以要在单元格所在列上设置(列的设置在工作表上),宽度数值好像是字符的1/256,所以
    256以便达到设置效果。

    //设置单元格的高度
    row.Height = 30 * 20;
    //设置单元格的宽度
    sheet.SetColumnWidth(0, 30 * 256);

    合并单元格
    //合并单元格实际上是声明一个区域,该区域中的单元格将进行合并,合并后的内容与样式以该区域最左上角的单元格为准。
    //设置一个合并单元格区域,使用上下左右定义CellRangeAddress区域
    //CellRangeAddress四个参数为:起始行,结束行,起始列,结束列
    sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));

    设置单元格数据格式
    cs.DataFormat = xwk.CreateDataFormat().GetFormat("0%");
    具体值见

    添加公式

    添加公式:使用Cell的CellFormula来设置公式,是一个字符串,公式前不需要加=号。
    //通过Cell的CellFormula向单元格中写入公式
    //注:直接写公式内容即可,不需要在最前加’=’
    ICell cell2 = sheet.CreateRow(1).CreateCell(0);
    cell2.CellFormula = “HYPERLINK(”测试图片.jpg”,”测试图片.jpg”)”;

    Dataset、DataGridView转换Excel帮助类

    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using NPOI.XSSF.UserModel;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace test
    {
        public class ExcelHelper : IDisposable
        {
            private string fileName = null; //文件名
            private IWorkbook workbook = null;
            private FileStream fs = null;
            private bool disposed = false;
            public ExcelHelper(string fileName)
            {
                this.fileName = fileName;
            }
     
            /// <summary>
            /// 将DataTable数据导入到excel中
            /// </summary>
            /// <param name="data">要导入的数据</param>
            /// <param name="isColumnWritten">DataTable的列名是否要导入</param>
            /// <param name="sheetName">要导入的excel的sheet的名称</param>
            /// <returns>导入数据行数(包含列名那一行)</returns>
            public int DataTableToExcel(DataTable data, string sheetName, bool isColumnWritten)
            {
                int i = 0;
                int j = 0;
                int count = 0;
                ISheet sheet = null;
     
                fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                    workbook = new XSSFWorkbook();
                else if (fileName.IndexOf(".xls") > 0) // 2003版本
                    workbook = new HSSFWorkbook();
     
                try
                {
                    if (workbook != null)
                    {
                        sheet = workbook.CreateSheet(sheetName);
                    }
                    else
                    {
                        return -1;
                    }
     
                    if (isColumnWritten == true) //写入DataTable的列名
                    {
                        IRow row = sheet.CreateRow(0);
                        for (j = 0; j < data.Columns.Count; ++j)
                        {
                            row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
                        }
                        count = 1;
                    }
                    else
                    {
                        count = 0;
                    }
     
                    for (i = 0; i < data.Rows.Count; ++i)
                    {
                        IRow row = sheet.CreateRow(count);
                        for (j = 0; j < data.Columns.Count; ++j)
                        {
                            row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
                        }
                        ++count;
                    }
                    workbook.Write(fs); //写入到excel
                    return count;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.Message);
                    return -1;
                }
            }
     
            /// <summary>
            /// 将DataTable数据导入到excel中
            /// </summary>
            /// <param name="data">要导入的数据</param>
            /// <param name="isColumnWritten">DataGridView的列名是否要导入</param>
            /// <param name="sheetName">要导入的excel的sheet的名称</param>
            /// <returns>导入数据行数(包含列名那一行)</returns>
            public int DataGridViewToExcel(DataGridView data, string sheetName, bool isColumnWritten)
            {
                int i = 0;
                int j = 0;
                int count = 0;
                ISheet sheet = null;
     
                fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                if (fileName.IndexOf(".xlsx") > 0) // 2007版本
                    workbook = new XSSFWorkbook();
                else if (fileName.IndexOf(".xls") > 0) // 2003版本
                    workbook = new HSSFWorkbook();
     
                try
                {
                    if (workbook != null)
                    {
                        sheet = workbook.CreateSheet(sheetName);
                    }
                    else
                    {
                        return -1;
                    }
     
                    if (isColumnWritten == true) //写入DataTable的列名
                    {
                        IRow row = sheet.CreateRow(0);
                        for (j = 0; j < data.Columns.Count-1; ++j)
                        {
                            row.CreateCell(j).SetCellValue(data.Columns[j+1].HeaderText);
                        }
                        count = 1;
                    }
                    else
                    {
                        count = 0;
                    }
     
                    for (i = 0; i < data.Rows.Count; ++i)
                    {
                        IRow row = sheet.CreateRow(count);
                        for (j = 0; j < data.Columns.Count-1; ++j)
                        {
                            row.CreateCell(j).SetCellValue(data.Rows[i].Cells[j+1].Value.ToString());
                        }
                        ++count;
                    }
                    workbook.Write(fs); //写入到excel
                    return count;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception: " + ex.Message);
                    return -1;
                }
            }
     
            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;
                }
            }
        }
    }
    
  • 相关阅读:
    登录验证servlet实现
    IOS开发(68)之捕获点击划屏手势
    jquery实现漂浮在网页右侧的qq在线客服插件
    深切缅怀“5.12”特大地震遇难同胞
    PHP实现微信申请退款流程实例源码
    empty和isset的区别
    ThinkPHP中实现微信支付(jsapi支付)流程
    php中正则表达式详解
    PHP与RBAC设计思路讲解与源码
    一起谈.NET技术,VS2010实践RUP4+1架构模型 狼人:
  • 原文地址:https://www.cnblogs.com/leestar54/p/5215573.html
Copyright © 2011-2022 走看看