package com.nt.test;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Iterator;

 

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

 

public class TestPOI {

//    public static void main(String[] args) {

//        File file=new File("D://nt/question/1/木工.xls");

//        try {

//            FileInputStream fint=new FileInputStream(file);

//            POIFSFileSystem poifsFileSystem=new POIFSFileSystem(fint);

//            HSSFWorkbook workbook=new HSSFWorkbook(poifsFileSystem);

//            HSSFSheet sheet=workbook.getSheetAt(0);

//            

//            

//            Iterator rows=sheet.rowIterator();

//            //得到总行数

//            int rowtotalnumber=sheet.getLastRowNum();

//            System.out.println("rowtotal=========="+rowtotalnumber);

//            while (rows.hasNext()) {

//                HSSFRow row=(HSSFRow) rows.next();

//                int rownumber=row.getRowNum();

//                //当前行数

//                System.out.println("rownumber------"+rownumber);

//                

//                //当前行的列数

//                int lie=row.getPhysicalNumberOfCells();

//                System.out.println("lie============"+lie);

//                

//                

//                Iterator cells=row.cellIterator();

//                

//                while(cells.hasNext()){

//                    HSSFCell cell=(HSSFCell) cells.next();

//                    

//                    //列好 从0开始

//                    int cellnumber=cell.getCellNum();

//                    System.out.println("cellnumber----"+cellnumber);

//                    System.out.println(getCellStringValue(cell));

//                }

//            }

//        } catch (FileNotFoundException e) {

//            e.printStackTrace();

//        } catch (IOException e) {

//            e.printStackTrace();

//        }

//    }

    

    
 

 

public static String getCellStringValue(HSSFCell cell) {

String cellValue = "";

switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_STRING://字符串类型

cellValue = cell.getStringCellValue();

if(cellValue.trim().equals("")||cellValue.trim().length()<=0)

cellValue=" ";

break;

case HSSFCell.CELL_TYPE_NUMERIC: //数值类型

cellValue = String.valueOf(cell.getNumericCellValue());

break;

case HSSFCell.CELL_TYPE_FORMULA: //公式

cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

cellValue = String.valueOf(cell.getNumericCellValue());

break;

case HSSFCell.CELL_TYPE_BLANK:

cellValue=" ";

break;

case HSSFCell.CELL_TYPE_BOOLEAN:

break;

case HSSFCell.CELL_TYPE_ERROR:

break;

default:

break;

}

return cellValue;

}

 

}

 

 

 

 

 

 

 

通过poi导出数据库的记录到excl表

 

 

package com.test.daotest;

 

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Iterator;

import java.util.List;

 

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.hibernate.Session;

import org.hibernate.Transaction;

 

import com.test.model.Question;

import com.test.until.HibernateSessionFactory;

 

public class ExportQuestion {

    public static void main(String[] args) {

        int id=14;

         try {

            HSSFWorkbook wb=new HSSFWorkbook();

             FileOutputStream fileout = new FileOutputStream("test"+id+".xls");

             wb.write(fileout);

            

             HSSFSheet sheet=wb.createSheet("new sheet");

             //通过Hibernate来查询addressbook_table表中的数据,将其存储在List中

                 Session s=HibernateSessionFactory.getSession();

             Transaction tx = s.beginTransaction();

             org.hibernate.Query query= s.createQuery("from Question q where q.majorId="+id);

             List list = query.list();

             tx.commit();

             int k =0;

 

             //创建表格,创建表格行和单元格,将数据库中表的字段存储在单元格中.

             for(Iterator it=list.iterator();it.hasNext();){

             Question q =(Question)it.next();

             HSSFRow row=sheet.createRow((short)k);

             row.createCell((short)0).setCellValue(1);

             row.createCell((short)1).setCellValue(q.getQuestion());

             row.createCell((short)2).setCellValue(q.getOptionA());

             row.createCell((short)3).setCellValue(q.getOptionB());

             row.createCell((short)4).setCellValue(q.getOptionC());

             row.createCell((short)5).setCellValue(q.getOptionD());

             row.createCell((short)6).setCellValue(q.getAnswer());

             row.createCell((short)7).setCellValue(q.getMajorId());

             row.createCell((short)8).setCellValue(0);

             row.createCell((short)9).setCellValue(0);

             k++;

             }

             FileOutputStream fileout1 = new FileOutputStream("test"+id+".xls");

             wb.write(fileout1);

            

             fileout1.close();

 

 

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

 

 

 

 

poi读取、通过poi导出数据库的记录到excl表的更多相关文章

  1. 如何使用mysqldump命令导入导出数据库下的数据或表结构(远程or本地都适合)

    不多说,直接上干货! https://www.cloudera.com/documentation/manager/5-1-x/Cloudera-Manager-Installation-Guide/ ...

  2. apache poi 读取xlsx并导出为json(没考虑xls)

    1.用到的jar包:fastjson-1.2.9.poi(poi-3.15.poi-ooxml-3.15.poi-ooxml-schemas-3.15.xmlbeans-2.6.0.commons-c ...

  3. linux导入导出数据库方法 windows导入导出数据库方法

    1.使用管理员账号(sys)登录查询字符集信息 第一步:查询LinuxOracle数据库的字符集 select userenv('language') from dual; 查询结果集可能为:AMER ...

  4. SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)

    读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...

  5. POI读取Excel数据保存到数据库,并反馈给用户处理信息(导入带模板的数据)

    今天遇到这么一个需求,将课程信息以Excel的形式导入数据库,并且课程编号再数据库中不能重复,也就是我们需要先读取Excel提取信息之后保存到数据库,并将处理的信息反馈给用户.于是想到了POI读取文件 ...

  6. jsp+servlet+poi导出数据库中的数据

    index.jsp: <a href="POIout">简单导出数据</a> <a href="POIoutTemplate"&g ...

  7. Poi读取Excle报错 java.util.zip.ZipException: invalid stored block lengths

    一:Poi读取Excle报错  java.util.zip.ZipException: invalid stored block lengths 系统中需要导出excle签收单,excle模板是预设好 ...

  8. apache POI 操作excel<导入导出>

    1.首先导入maven依赖 <!-- POI核心依赖 --> <dependency> <groupId>org.apache.poi</groupId> ...

  9. lucent检索技术之创建索引:使用POI读取txt/word/excel/ppt/pdf内容

    在使用lucent检索文档时,必须先为各文档创建索引.索引的创建即读出文档信息(如文档名称.上传时间.文档内容等),然后再经过分词建索引写入到索引文件里.这里主要是总结下读取各类文档内容这一步. 一. ...

随机推荐

  1. 【LeetCode43】 Multiply Strings

    题目描述: 解题思路: java代码: public class LeetCode43 { public static void main(String[] args) { String num1=& ...

  2. #leetcode刷题之路49-字母异位词分组

    给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串.示例:输入: ["eat", "tea", "tan" ...

  3. PHP分行打印数组-php输出数组方法大全

    我们都知道php有两种方式可以打印数组 $arr = array( "a"=>"orange", "b"=>"bana ...

  4. HDFS命令实现分析

    HDFS命令概述 HDFS命令涉及两类,一类是hadoop命令,一类是hdfs命令,功能也分为两类,第一类是HDFS文件操作命令,第二类是HDFS管理命令. 二者都是shell命令,真正的命令只有ha ...

  5. python3爬虫-通过selenium登陆拉钩,爬取职位信息

    from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from se ...

  6. Go语言相对于C++的优点

    Go语言是Google公司在2009年开源的一门高级编程语言,它为解决大型系统开发过程中的实际问题而设计,支持并发.规范统一.简单优雅,被很多Go语言传道者誉为“互联网时代的C语言”.而C++语言诞生 ...

  7. 5.Control flow statements-流程控制(Dart中文文档)

    你可以使用如下流程控制符: if and else for loops while and do-while loops break and continue switch and case asse ...

  8. WPF的IsSynchronizedWithCurrentItem属性

    如果两个控件都绑定到同一个源(ObservableCollection)集合视图时,该对象会自动绑定到该视图的 CurrentItem.请注意,CollectionViewSource 对象会自动同步 ...

  9. http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/

    http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/

  10. react canvas

    http://engineering.flipboard.com/2015/02/mobile-web/