项目报表要做导出Excel的功能,用了POI,首先要加载POI所需要的jar包,自己百度,直接贴代码
简单吧,刚开始以为很难呢,其实没想像中的那么复杂,多动手操作啥都好说了,其他属性,自己百度吧!
import org.apache.poi.hssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
public class exportExcel2
{
public static void main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象
HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象
// Create a row and put some cells in it. Rows are 0 based.
HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式
String names[]={"aaa","bbb","ccc","ddd"};//从后台查出来的结果集
String email[]={"aaaa","bbbb","cccc","dddd"};
String phone[]={"aaaaa","bbbbb","ccccc","ddddd"};
HSSFRow row;
HSSFCell cell ;
//标题行
row=sheet.createRow(0);
cell= row.createCell(0);//建立新cell
cell.setCellValue("姓名");//设置cell的整数类型的值
cell = row.createCell(1);//建立新cell
cell.setCellValue("性别");//设置cell的整数类型的值
cell = row.createCell(2);//建立新cell
cell.setCellValue("年龄");//设置cel
for(int i=1;i<5;i++){
row = sheet.createRow(i);//建立新行
for(int j=i-1;j<i;j++){
System.out.println(i+"****"+j);
cell= row.createCell(0);//建立新cell
cell.setCellValue(names[j]);//设置cell的整数类型的值
cell = row.createCell(1);//建立新cell
cell.setCellValue(email[j]);//设置cell的整数类型的值
cell = row.createCell(2);//建立新cell
cell.setCellValue(phone[j]);//设置cell的整数类型的值
}
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("c:/workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}
简单吧,刚开始以为很难呢,其实没想像中的那么复杂,多动手操作啥都好说了,其他属性,自己百度吧!