BabyBaby
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using System.Data;
using System.Data.SqlClient;
 
namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            //HSSFWorkbook book = new HSSFWorkbook();//创建工作薄
            //ISheet sheet = book.CreateSheet("学生信息");//创建一个表
            ////在NPOI里,Row和Cell是表结构里的基本元素,必须先通过Create函数创建才能使用
            //IRow row = sheet.CreateRow(0);//在第一行创建行,在NPOI里的0就是代表这Excel里面的1
            //ICell cell = row.CreateCell(0);//在第一行的第一列创建单元格
            //cell.SetCellValue("xx");//给单元格赋值,他可以传的参数类型有:bool string double DateTime IRichTextString
            ////也可以采用以下创建方式 获得指定的行和列 表对象.GetRow(int rownum).GetCell(int cellnum).SetCellValue("要编辑的值");
            //sheet.GetRow(0).GetCell(0).SetCellValue("xx");
            ////新建一个行和列并赋值
            //sheet.CreateRow(0).CreateCell(0).SetCellValue("aa");
            ////编辑完毕之后保存
            //FileStream fs = new FileStream(@"test.xls", FileMode.Create);
            //book.Write(fs);
            //fs.Close();
            //Console.WriteLine("..");
            //DataTable dt = DBHelper.GetData("select * from Student");
            //HSSFWorkbook book = Creat("学生信息", dt);
            //FileStream fs = new FileStream(@"test.xls", FileMode.Create);
            //book.Write(fs);
            //fs.Close();
            //Console.WriteLine("..");
            FileStream fs = new FileStream(@"test.xls", FileMode.Open, FileAccess.Read);
            HSSFWorkbook book = new HSSFWorkbook(fs);
            ISheet sheet = book.GetSheet("学生信息");
            ICell ic = sheet.GetRow(0).GetCell(7);
            Console.Write(ic.CellType);
               HSSFWorkbook book = new HSSFWorkbook();
               //合并单元格
            cell.SetCellValue("Sales Report");
            sheet.AddMergedRegion(new CellRangeAddress(2,4,1,6));//合并单元格,参数::1.开始的行,2.结束的列,3.开始的列,4.结束的列
               //ICellStyle style = book.CreateCellStyle();
            //水平对齐
            //左对齐
            //style.Alignment = HorizontalAlignment.Left;
            //cell.CellStyle = style;
            //右对齐
            //style.Alignment = HorizontalAlignment.Right;
            //cell.CellStyle = style;
            //居中
            //style.Alignment = HorizontalAlignment.Center;
            //cell.CellStyle = style;
            //  sheet.AddMergedRegion(new CellRangeAddress(2,4,1,6));//合并单元格,参数::1.开始的行,2.结束的列,3.开始的列,4.结束的列,索引都是从0开始
            //6.设置列宽   SetColumnWidth(列的索引号从0开始, N * 256) 第二个参数的单位是1/256个字符宽度。例:将第四列宽度设置为了30个字符。  
            //sheet.SetColumnWidth(3, 30 * 256);
            //7.设置行高   Height的单位是1/20个点。例:设置高度为50个点  
            //row.Height = 50 * 20;
        }
        public static HSSFWorkbook Creat(string SheetName,DataTable dt)
        {
            HSSFWorkbook book = new HSSFWorkbook();//创建一个工作薄对象
            ISheet sheet = book.CreateSheet(SheetName);//创建一个表
                               //取得列宽
                //创建一个长度为列个数的数组
            int[] arrColWidth = new int[dt.Columns.Count];
            foreach (DataColumn item in dt.Columns)
            {
                arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length;
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    int intTemp = Encoding.GetEncoding(936).GetBytes(dt.Rows[i][j].ToString()).Length;
                    if (intTemp > arrColWidth[j])
                    {
                        arrColWidth[j] = intTemp;
                    }
                }
            }
            foreach (DataColumn item in dt.Columns)//循环设置列宽
            {
                sheet.SetColumnWidth(item.Ordinal, (arrColWidth[item.Ordinal] + 1) * 256);
            }
                                int index = 0;//初始值
            IRow row = sheet.CreateRow(0);//创建一个新行用来放列名
            foreach (DataColumn item in dt.Columns)//循环列
            {
                ICell cell = row.CreateCell(index);//创建列
                cell.SetCellValue(item.ColumnName);//把DataTable里的列明赋值给单元格
                index++;
            }
            for (int i = 0; i < dt.Rows.Count; i++)//循环DataTable里的行数
            {
                IRow rows = sheet.CreateRow(i);//创建行
                for (int j = 0; j < dt.Columns.Count; j++)//循环列数
                {
                    ICell cells = rows.CreateCell(j);//创建列
                    switch (dt.Rows[i][j].GetType().FullName)//判断值的类型
                    {
                        case "System.Boolean"://转布尔
                            cells.SetCellValue(bool.Parse(dt.Rows[i][j].ToString()));
                            break;
                        case "System.String"://转字符串
                            cells.SetCellValue(dt.Rows[i][j].ToString());
                            break;
                        case "System.DateTime"://转日期
                            cells.SetCellValue(DateTime.Parse(dt.Rows[i][j].ToString()).ToString("yyyy-MM-dd"));
                            break;
                        case "System.Double"://转double
                        case "System.Int32":
                            cells.SetCellValue(Double.Parse(dt.Rows[i][j].ToString()));
                            break;
                        default://如果为空则转成空
                            cells.SetCellValue("");
                            break;
                    }
                }
            }
            return book;
        }
    }
}

 

分类:

技术点:

相关文章: