初学Poi

创建一个web项目(PoiDemo)
导入jar包poi-3.9-20121203.jar
下载地址http://www.itmop.com/downinfo/177782.html

在这里插入图片描述
Demo1
package com.java.poi;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

public class Demo1 {

public static void main(String[] args) throws Exception {
	Workbook wb=new HSSFWorkbook();//定义一个新的工作簿
	FileOutputStream fileOut=new FileOutputStream("f://用Poi搞出来的工作簿.xls");
	wb.write(fileOut);
	fileOut.close();
}

}

Demo2

package com.java.poi;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

public class Demo2 {
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();//定义一个新的工作簿
wb.createSheet(“第一个sheet页”);//创建一个sheet页
wb.createSheet(“第二个sheet页”);//创建二个sheet页
FileOutputStream fileOut=new FileOutputStream(“f://用Poi搞出来的工作簿sheet.xls”);
wb.write(fileOut);
fileOut.close();
}
}
Demo3
package com.java.poi;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo3 {

public static void main(String[] args) throws Exception {
	Workbook wb=new HSSFWorkbook();//定义一个新的工作簿
	Sheet sheet=wb.createSheet("第一个sheet页");//创建一个sheet页
    Row row=sheet.createRow(0);//创建一行
    Cell cell=row.createCell(0);//创建一个单元格 第1列
    cell.setCellValue(1);//给单元格设置
    row.createCell(1).setCellValue(1.2);//创建一个单元格第二列值1.2
    row.createCell(2).setCellValue("这是一个字符串类型");//创建一个单元格第三列
    row.createCell(3).setCellValue(false);//创建一个单元格第四列值为布尔类型
    FileOutputStream fileOut=new FileOutputStream("f://用Poi搞出来的工作簿Cell.xls");
	wb.write(fileOut);
	fileOut.close();
}

}
Demo4
package com.java.poi;

import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo4 {

public static void main(String[] args) throws Exception{
	Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
	Sheet sheet=wb.createSheet("第一个Sheet页");  // 创建第一个Sheet页
	Row row=sheet.createRow(0); // 创建一个行
	Cell cell=row.createCell(0); // 创建一个单元格  第1列
	cell.setCellValue(new Date());  // 给单元格设置值
	
	CreationHelper createHelper=wb.getCreationHelper();
	CellStyle cellStyle=wb.createCellStyle(); //单元格样式类
	cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyy-mm-dd hh:mm:ss"));
	cell=row.createCell(1); // 第二列
	cell.setCellValue(new Date());
	cell.setCellStyle(cellStyle);
	
	cell=row.createCell(2);  // 第三列
	cell.setCellValue(Calendar.getInstance());
	cell.setCellStyle(cellStyle);
	
	FileOutputStream fileOut=new FileOutputStream("F:\\poi\\工作簿.xls");
	wb.write(fileOut);
	fileOut.close();
}

}

运行在F盘查看即可要是没有F盘把路径改成其他盘查看

猜你喜欢

转载自blog.csdn.net/anhldd/article/details/82889535
poi