apache-poi 操作excel
参照 :http://poi.apache.org/spreadsheet/quick-guide.htmlpackage com.xiexh.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import org.apache.poi.ss.usermodel.Cell;
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.WorkbookFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.examples.CreateCell;
public class NewWorkBook {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
readFile();
}
public static void createNewWorkBook1() throws Exception{
Workbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
public static void createNewWorkBook2() throws Exception{
Workbook wxssb = new XSSFWorkbook();
FileOutputStream xsfOut = new FileOutputStream("workbook.xlsx");
wxssb.write(xsfOut);
xsfOut.close();
}
public static void createNewSheet() throws Exception{
Workbook wb = new HSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
Sheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("aaa.xls");
wb.write(fileOut);
fileOut.close();
}
public static void createNewSheet1() throws Exception{
Workbook wb = new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
Sheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("bbb.xlsx");
wb.write(fileOut);
fileOut.close();
}
public static void CreatNewCell()throws Exception{
Workbook wb = new HSSFWorkbook();
CreationHelper creationHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
for(int i = 0;i<20;i++){
Row row = sheet.createRow(i);
for(int j=0;j<5;j++){
Cell cell = row.createCell(j);
if(i!=0){
cell.setCellValue("wo ai ni:"+i+"X"+j);
}else{
cell.setCellValue("第"+j+"列");
}
}
}
FileOutputStream fileOut = new FileOutputStream("ccc.xls");
wb.write(fileOut);
fileOut.close();
}
public static void createDifferentofCells() throws Exception{
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short)2);
row.createCell(0).setCellValue(1.1);
row.createCell(1).setCellValue(new Date());
row.createCell(2).setCellValue(Calendar.getInstance());
row.createCell(3).setCellValue("a string");
row.createCell(4).setCellValue(true);
row.createCell(5).setCellType(Cell.CELL_TYPE_ERROR);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("ddd.xls");
wb.write(fileOut);
fileOut.close();
}
public static void readFile()throws Exception{
Workbook wb = WorkbookFactory.create( new File("ccc.xls"));
Sheet sheet = wb.getSheetAt(0);
for(Row row : sheet){
for(Cell cell : row )
{
System.out.print(cell.getStringCellValue());
}
System.out.println();
}
}
}
页:
[1]