NPOI学习代码

本文介绍如何使用NPOI库将SQL查询结果导出为Excel文件,包括初始化工作簿、读取模板、创建样式、设置单元格数据等步骤。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.SS.UserModel;
using 大容量计量软件;


namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static HSSFWorkbook hssfworkbook;
        static void WriteToFile()
        {
            //Write the stream data of workbook to the root directory
            FileStream file = new FileStream(@"test.xls", FileMode.Create);
            hssfworkbook.Write(file);
            file.Close();
        }


        static void InitializeWorkbook()
        {
            //read the template via FileStream, it is suggested to use FileAccess.Read to prevent file lock.
            //book1.xls is an Excel-2007-generated file, so some new unknown BIFF records are added. 
            FileStream file = new FileStream(@"template/Book1.xls", FileMode.Open, FileAccess.Read);


            hssfworkbook = new HSSFWorkbook(file);


            //create a entry of DocumentSummaryInformation
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "NPOI Team";
            hssfworkbook.DocumentSummaryInformation = dsi;


            //create a entry of SummaryInformation
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "NPOI SDK Example";
            hssfworkbook.SummaryInformation = si;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            InitializeWorkbook();
            ISheet sheet1 = hssfworkbook.GetSheet("Sheet1");
            //create cell on rows, since rows do already exist,it's not necessary to create rows again.
            DataOperate dp = new DataOperate();
            string s1 = "select * from tb_11111_01";
            DataSet ds1 = new DataSet();
            ds1 = dp.getDs(s1, "分米表");
            DataTable dt = ds1.Tables["分米表"];
            int n = dt.Rows.Count;


            ICellStyle style1 = hssfworkbook.CreateCellStyle();
            style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
            IFont font1 = hssfworkbook.CreateFont();
            font1.FontName = "Times New Roman";
            font1.FontHeightInPoints = (short)14;
            style1.SetFont(font1);
            style1.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
            style1.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
            style1.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
            style1.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;


            for (int i = 0; i < 30; i++)
            {
                IRow rr = sheet1.CreateRow(i + 2);
                //rr.HeightInPoints = 50;
                ICell cc0 = rr.CreateCell(0, CellType.STRING);
                cc0.SetCellValue(dt.Rows[i][0].ToString());
                //cc0.CellStyle = style1;
                ICell cc1 = rr.CreateCell(1, CellType.STRING);
                cc1.SetCellValue(dt.Rows[i][1].ToString());
                //cc1.CellStyle = style1;
                ICell cc2 = rr.CreateCell(2, CellType.STRING);
                cc2.SetCellValue(dt.Rows[i][2].ToString());
                //cc2.CellStyle = style1;
                ICell cc3 = rr.CreateCell(3, CellType.STRING);
                cc3.SetCellValue(dt.Rows[i][3].ToString());
                //cc3.CellStyle = style1;
                ICell cc4 = rr.CreateCell(4, CellType.STRING);
                cc4.SetCellValue(dt.Rows[i][4].ToString());
                //cc4.CellStyle = style1;
                ICell cc5 = rr.CreateCell(5, CellType.STRING);
                cc5.SetCellValue(dt.Rows[i][5].ToString());
                //cc5.CellStyle = style1;
                ICell cc6 = rr.CreateCell(6, CellType.STRING);
                cc6.SetCellValue(dt.Rows[i][6].ToString());
                //cc6.CellStyle = style1;
                ICell cc7 = rr.CreateCell(7, CellType.STRING);
                cc7.SetCellValue(dt.Rows[i][7].ToString());
                //cc7.CellStyle = style1;
                ICell cc8 = rr.CreateCell(8, CellType.STRING);
                cc8.SetCellValue(dt.Rows[i][8].ToString());
                //cc8.CellStyle = style1;
                ICell cc9 = rr.CreateCell(9, CellType.STRING);
                cc9.SetCellValue(dt.Rows[i][9].ToString());
                //cc9.CellStyle = style1;
            }
            WriteToFile();
            MessageBox.Show("dssf");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值