您的位置:首页 > 编程语言 > Java开发

POI实战-java开发excel详解(第一章 简单写出)

2011-09-01 14:44 495 查看

1.4 POI简单写出Excel

代码部分:

package com.vintage.testpoi;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
* POI入门 :简单写出excel数据
* @author VintageYu
*
*/
public class WriteExcelTest {

public static void write(OutputStream outputStream) throws IOException{
//初始一个workbook
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个表
HSSFSheet sheet = workbook.createSheet("firstSheet");
//创建行
HSSFRow row = sheet.createRow(0);
//创建单元格
HSSFCell cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString("hello POI"));
workbook.write(outputStream);
}

public static void main(String[] args) {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(new File("E:\\helloPOI.xls"));
write(outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

}




图4

以下贴近实际进一步修改:

从数据库中读出相关信息并写入到Excel中

数据库表如图:



图5

主要类:
package com.vintage.testpoi;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

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;

/**
* POI入门 :简单写出excel数据
* @author VintageYu
*
*/
public class WriteExcelTest {

public static void write(OutputStream outputStream) throws IOException{
//初始一个workbook
HSSFWorkbook workbook = new HSSFWorkbook();
List<Student> list = Conn.getData();
//循环创建多个sheet
for(int sheetIndex = 0; sheetIndex < 3; sheetIndex++){
HSSFSheet sheet = workbook.createSheet("sheet"+sheetIndex);
//创建多行
for(int rowIndex = 0; rowIndex < list.size(); rowIndex++){
HSSFRow row = sheet.createRow(rowIndex);
Student student = list.get(rowIndex);
//创建多列
for(int cellnum = 0; cellnum < 4; cellnum++){
HSSFCell cell = row.createCell(cellnum);
switch (cellnum) {
case 0:
cell.setCellValue(student.getName());
break;
case 1:
cell.setCellValue(student.getNo());
break;
case 2:
cell.setCellValue(student.getNativePlace());
break;
case 3:
cell.setCellValue(student.getEdu());
break;
}
}
}
}
workbook.write(outputStream);
}

public static void main(String[] args) {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(new File("E:\\helloPOI.xls"));
write(outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

}


依赖类:

public class Conn {

public static Connection getConn(){
Connection cn = null;
try {
Class.forName( "oracle.jdbc.driver.OracleDriver" );
cn = DriverManager.getConnection( "jdbc:oracle:thin:@****:1521:***", "***", "***");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return cn;
}

public static List<Student> getData(){
List<Student> list = new ArrayList<Student>();
Connection conn = getConn();
String sql = "SELECT * FROM tpoi_vintage t";
try {
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Integer id = rs.getInt("id");
String name = rs.getString("name");
String no = rs.getString("no");
String nativePlace = rs.getString("native");
String edu = rs.getString("edu");
Integer year = rs.getInt("YEAR");
Integer math = rs.getInt("MATH");
Integer chinese = rs.getInt("CHINESE");
Integer english = rs.getInt("ENGLISH");
Integer science = rs.getInt("SCIENCE");
Integer isCity = rs.getInt("ISCITY");
Date schoolDate = rs.getDate("SCHOOLDATE");
Date birth = rs.getDate("BIRTH");

Student student = new Student();
student.setId(id);
student.setName(name);
student.setNo(no);
student.setNativePlace(nativePlace);
student.setEdu(edu);
student.setYear(year);
student.setMath(math);
student.setChinese(chinese);
student.setEnglish(english);
student.setScience(science);
if(isCity == 0){
student.setCity(false);
}else if(isCity == 1){
student.setCity(true);
}
student.setSchoolDate(schoolDate);
student.setBirth(birth);

list.add(student);

}
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}

public class Student {
private Integer id;
private String name;
private String no;
private String nativePlace;
private String edu;
以下省略。。。


写出Excel预览:



图6

ps:POI实战pdf版下载 http://download.csdn.net/source/3566377
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: