import java.io.File;
import java.io.IOException;
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.ss.usermodel.Cell;
public class WriteExcel {
public static void main(String[] args) {
String title[] = { "id", "name", "age" };
// 创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建sheet表
HSSFSheet sheet = workbook.createSheet();
// 创建标题
HSSFRow titleRow = sheet.createRow(0);
for (int i = 0; i < title.length; i++) {
Cell cell = titleRow.createCell(i);
cell.setCellValue(title[i]);
}
for(int i=1;i<20;i++){
HSSFRow hssfRow= sheet.createRow(i);
for(int j=0;j<3;j++){
Cell cell= hssfRow.createCell(j);
cell.setCellValue("你好"+i+j);
}
}
File file =new File("D:/b.xls");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
workbook.write(file);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(workbook!=null){
workbook.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
workbook =null;
}
}
}
}
import java.io.IOException;
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.ss.usermodel.Cell;
public class WriteExcel {
public static void main(String[] args) {
String title[] = { "id", "name", "age" };
// 创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建sheet表
HSSFSheet sheet = workbook.createSheet();
// 创建标题
HSSFRow titleRow = sheet.createRow(0);
for (int i = 0; i < title.length; i++) {
Cell cell = titleRow.createCell(i);
cell.setCellValue(title[i]);
}
for(int i=1;i<20;i++){
HSSFRow hssfRow= sheet.createRow(i);
for(int j=0;j<3;j++){
Cell cell= hssfRow.createCell(j);
cell.setCellValue("你好"+i+j);
}
}
File file =new File("D:/b.xls");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
workbook.write(file);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(workbook!=null){
workbook.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
workbook =null;
}
}
}
}