package excel;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class excelTest {
public void exportExcel() throws IOException {
//创建 一个 文件
HSSFWorkbook wb= new HSSFWorkbook();
//创建一个 sheet
HSSFSheet hssfSheet = wb.createSheet("员工表");
//
HSSFRow row = hssfSheet.createRow(0);
//创建单元格 并且设置为 居中格式
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = row.createCell(0);
cell.setCellValue("姓名");
cell.setCellStyle(cellStyle);
cell = row.createCell(1);
cell.setCellValue("性别");
cell.setCellStyle(cellStyle);
cell = row.createCell(2);
cell.setCellValue("年龄");
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("联系方式");
cell.setCellStyle(cellStyle);
// 数据
HSSFRow row2 = hssfSheet.createRow(1);
row2.createCell(0).setCellValue("wang");
row2.createCell(1).setCellValue("nan");
row2.createCell(2).setCellValue("23");
row2.createCell(3).setCellValue("6545646521");
// 数据
HSSFRow row3 = hssfSheet.createRow(2);
row3.createCell(0).setCellValue("yang");
row3.createCell(1).setCellValue("nv");
row3.createCell(2).setCellValue("23");
row3.createCell(3).setCellValue("1845641232");
FileOutputStream out =new FileOutputStream("E:/员工表.xls");
wb.write(out);
out.close();
}
public void importExcel() throws IOException{
FileInputStream in =new FileInputStream("E:/员工表.xls");
HSSFWorkbook workbook = new HSSFWorkbook(in);
HSSFSheet sheet = workbook.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
int rowNum =0;
while (rowNum <=lastRowNum) {
HSSFRow row =sheet.getRow(rowNum);
int cellNum = 0;
int lastcellNum = row.getLastCellNum();
while (cellNum <lastcellNum) {
HSSFCell cell = row.getCell(cellNum);
System.out.println(cell.getStringCellValue());
cellNum ++;
}
rowNum ++;
}
}
public static void main(String[] args) throws IOException {
excelTest test = new excelTest();
//生成 excel
test.exportExcel();
//解析 excel
test.importExcel();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class excelTest {
public void exportExcel() throws IOException {
//创建 一个 文件
HSSFWorkbook wb= new HSSFWorkbook();
//创建一个 sheet
HSSFSheet hssfSheet = wb.createSheet("员工表");
//
HSSFRow row = hssfSheet.createRow(0);
//创建单元格 并且设置为 居中格式
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCell cell = row.createCell(0);
cell.setCellValue("姓名");
cell.setCellStyle(cellStyle);
cell = row.createCell(1);
cell.setCellValue("性别");
cell.setCellStyle(cellStyle);
cell = row.createCell(2);
cell.setCellValue("年龄");
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("联系方式");
cell.setCellStyle(cellStyle);
// 数据
HSSFRow row2 = hssfSheet.createRow(1);
row2.createCell(0).setCellValue("wang");
row2.createCell(1).setCellValue("nan");
row2.createCell(2).setCellValue("23");
row2.createCell(3).setCellValue("6545646521");
// 数据
HSSFRow row3 = hssfSheet.createRow(2);
row3.createCell(0).setCellValue("yang");
row3.createCell(1).setCellValue("nv");
row3.createCell(2).setCellValue("23");
row3.createCell(3).setCellValue("1845641232");
FileOutputStream out =new FileOutputStream("E:/员工表.xls");
wb.write(out);
out.close();
}
public void importExcel() throws IOException{
FileInputStream in =new FileInputStream("E:/员工表.xls");
HSSFWorkbook workbook = new HSSFWorkbook(in);
HSSFSheet sheet = workbook.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
int rowNum =0;
while (rowNum <=lastRowNum) {
HSSFRow row =sheet.getRow(rowNum);
int cellNum = 0;
int lastcellNum = row.getLastCellNum();
while (cellNum <lastcellNum) {
HSSFCell cell = row.getCell(cellNum);
System.out.println(cell.getStringCellValue());
cellNum ++;
}
rowNum ++;
}
}
public static void main(String[] args) throws IOException {
excelTest test = new excelTest();
//生成 excel
test.exportExcel();
//解析 excel
test.importExcel();
}
}