标签:
①:向excel输出东西
package poi;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
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 Demo1 implements Runnable{
public static void main(String[] args) throws Exception {
Demo1 demo1=new Demo1();
Thread thread=new Thread(demo1);
thread.start();
}
@Override
public void run() {
Workbook wb=new HSSFWorkbook();
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream("E://poi.xls"); //文件位置
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Sheet sheet=wb.createSheet(); //创建一页
Row row=sheet.createRow(0); //创建一行(第一行)
Cell cell1=row.createCell(0); //创建一列(第一列)
Cell cell2=row.createCell(1);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cell1.setCellValue(format.format(new Date()));
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
cell2.setCellValue(format.format(new Date()));
System.out.println("!!!!");
try {
wb.write(outputStream); //向工作簿写东西
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
outputStream.close(); // 关闭流
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
②:poi处理日期:
package 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 Demo2 {
public static void main(String[] args) throws Exception {
Workbook wb=new HSSFWorkbook();
FileOutputStream outputStream=new FileOutputStream("E://poi2.xls");
Sheet sheet=wb.createSheet();
Row row=sheet.createRow(0);
Cell cell=row.createCell(0);
cell.setCellValue(new Date());
//第一种方法
CreationHelper helper=wb.getCreationHelper();
CellStyle cellStyle=wb.createCellStyle();
cellStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-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);
wb.write(outputStream);
outputStream.close();
}
}
③:利用for循环遍历
package poi;
import java.io.FileInputStream;
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 Demo3 {
public static void main(String[] args) throws Exception {
FileInputStream inputStream=new FileInputStream("E://poi.xls");
POIFSFileSystem fs=new POIFSFileSystem(inputStream);
HSSFWorkbook wb=new HSSFWorkbook(fs); //获取工作簿
HSSFSheet sheet=wb.getSheetAt(0); //获取第一页
if(sheet==null){
return;
}
//循环遍历所有的行
for(int rowNum=0;rowNum<=sheet.getLastRowNum();rowNum++){
HSSFRow row=sheet.getRow(rowNum);
//如果行为空就跳过
if(row==null){
continue;
}
//循环遍历所有的列
for(int cellNum=0;cellNum<=row.getLastCellNum();cellNum++){
HSSFCell cell=row.getCell(cellNum);
//如果行为列就跳过
if(cell==null){
continue;
}
System.out.print(" "+getValue(cell));
}
System.out.println();
}
}
//获取并处理单元格的值
private static String getValue(HSSFCell hssfCell){
if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN){
return String.valueOf(hssfCell.getBooleanCellValue()); //boolean类型
}else if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
return String.valueOf(hssfCell.getNumericCellValue()); //数字型
}else{
return String.valueOf(hssfCell.getStringCellValue()); //字符串型
}
}
}
④:不用for循环遍历
package poi;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.extractor.ExcelExtractor;
public class Demo4 {
public static void main(String[] args) throws Exception {
FileInputStream inputStream=new FileInputStream("E://poi.xls");
POIFSFileSystem fs=new POIFSFileSystem(inputStream);
HSSFWorkbook wb=new HSSFWorkbook(fs);
ExcelExtractor extractor=new org.apache.poi.hssf.extractor.ExcelExtractor(wb);
System.out.println(extractor.getText());
}
}
标签:
原文地址:http://www.cnblogs.com/dmchzp/p/5202523.html