excel导入、导出

http://blog.csdn.net/cjh200102/article/details/12557599

NPOI

工作本HSSFWorkbook

  构造方法,无参表示创建一个新的工作本,可以接收一个流用于打开一个现有的工作本

  方法CreateSheet(索引):创建指定索引的sheet对象

  方法GetSheetAt(索引):根据索引获取sheet对象

  方法CreateCellStyle():创建单元格样式对象

  方法CreateFont():创建字体对象

  方法Write(stream):将工作本输出到流中

工作表HSSFSheet

  方法CreateRow(索引):创建指定索引的行

  方法GetRow(索引):根据索引获取行

  方法AddMergedRegion():设置合并区域,参数包括开始行索引、开始列索引、结束行索引、结束列索引

  方法SetColumnWidth(索引,宽度):设置指定列的宽度,单位是一个字符宽度的256分之1(一个汉字占用两个字符)

  属性FirstRowNum、LastRowNum:获取第一行、最后一行的索引值

行HSSFRow

  方法CreateCell(索引):创建指定索引的行

  方法GetCell(索引):根据索引获取单元格

  属性HeightInPoints:指定或设置高度

单元格HSSFCell

  方法SetCellValue():设置单元格中的值

  属性***CellValue:获取单元格中指定类型的值,如果类型不匹配则抛异常

  属性CellStyle:获取或设置单元格样式

单元格样式HSSFCellStyle

  方法SetFont(字体对象):设置字体样式

  属性Alignment:水平对齐,1左,2中,3右

字体对象HSSFFont

  属性FontHeightInPoints:获取或设置字体大小

  属性Boldweight:获取或设置字体加粗

导出

//创建一个工作本
            HSSFWorkbook wb = new HSSFWorkbook();
            //创建sheet
            ISheet sheet = wb.CreateSheet("测试");
            //设置头合并单元格
            sheet.AddMergedRegion(new CellRangeAddress(0,0,0,1));
            //创建row
            IRow row0 = sheet.CreateRow(0);
            //创建cell
            ICell cell = row0.CreateCell(0);
            //为cell设置值
            cell.SetCellValue ("demo");
            //创建cellStyle
            ICellStyle cellStyle = wb.CreateCellStyle();
            //字体居中
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            //为cell添加样式
            cell.CellStyle = cellStyle;
            //创建标题行
            IRow row1 = sheet.CreateRow(1);
            //创建cell
            ICell cell0 = row0.CreateCell(0);
            cell0.SetCellValue("编号");
            cell0.CellStyle = cellStyle;
            ICell cell1 = row0.CreateCell(1);
            cell1.SetCellValue("类型");
            cell1.CellStyle = cellStyle;
            //设置创建行的索引,因为已经创建过两行,所以从索引2开始创建
            int index = 2;
            //遍历datatable创建row、cell
            foreach (DataRow item in dt.Rows)
            {
                IRow row = sheet.CreateRow(index++);
                ICell cellFirst = row.CreateCell(0);
                cellFirst.SetCellValue(item["BookTypeId"].ToString());
                ICell cellSecond = row.CreateCell(1);
                cellSecond.SetCellValue(item["BookTypeName"].ToString());
            }
            //创建或打开文件
            using (FileStream fs = new FileStream(@"D:UsersDesktopdemo.xls",FileMode.OpenOrCreate))
            {
                //往文件里写数据
                wb.Write(fs);
         fs.Close(); }

导入

 List<BookType> list = new List<BookType>();
            using (FileStream fs=new FileStream(@"D:UsersDesktopdemo.xls",FileMode.OpenOrCreate))
            {
                HSSFWorkbook wb = new HSSFWorkbook(fs);
                ISheet sheet = wb.GetSheetAt(0);
                int index = 2;
                IRow row = sheet.GetRow(2);
                while (row!=null)
                {
                    list.Add(new BookType()
                    {
                        BookTypeId = Convert.ToInt32(row.GetCell(0).StringCellValue),
                        BookTypeName = row.GetCell(1).StringCellValue
                    });
                    row = sheet.GetRow(index++);
                }
原文地址:https://www.cnblogs.com/xiaonangua/p/7428867.html