Excel--java POi

import java.io.File;
import java.io.FileOutputStream; import org.apache.commons.io.FileUtils;
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; public class PoiExpExcel {
public static void main(String[] args) {
String[] title={"ID","用户","密码"};
//创建Excel工作薄
HSSFWorkbook workbook=new HSSFWorkbook();
//创建一个工作表sheet
HSSFSheet sheet=workbook.createSheet();
//创建第一行
HSSFRow row= sheet.createRow(0);
HSSFCell cell=null;
//插入第一行数据
for (int i = 0; i < title.length; i++) {
cell=row.createCell(i);
cell.setCellValue(title[i]);
}
//追加数据
for (int i = 0; i < 10; i++) {
HSSFRow nextrow=sheet.createRow(i);
//第一个单元格
HSSFCell cell2=nextrow.createCell(0);
cell2.setCellValue("a"+i);
//第二个单元格
cell2=nextrow.createCell(1);
cell2.setCellValue("小明"+i);
//第三个单元格
cell2=nextrow.createCell(2);
cell2.setCellValue("123456");
}
//创建一个文件
File file=new File("e:/poi_test.xls");
try {
file.createNewFile();
//写入硬盘
FileOutputStream stream=FileUtils.openOutputStream(file);
workbook.write(stream);
stream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

这里是导出的代码 直接贴上,下面的是导入

import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils;
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; public class PoiReaderExcel {
public static void main(String[] args) {
File file=new File("e:/poi_test.xls");
try {
//创建Excel,读取文件
HSSFWorkbook workbook=new HSSFWorkbook(FileUtils.openInputStream(file));
//获取第一张工作表 :通过名字获取
// HSSFSheet sheet=workbook.getSheet("sheet0");
//读取默认的第一张工作页
HSSFSheet sheet=workbook.getSheetAt(0);
//读取分步骤详细
int FirstRowNum=sheet.getFirstRowNum();
int LastRowNum=sheet.getLastRowNum();
for (int i = FirstRowNum; i < LastRowNum; i++) {
HSSFRow row=sheet.getRow(i);
int FirstCellNum=row.getFirstCellNum();
int LastCellNum=row.getLastCellNum();
for (int j = FirstCellNum; j < LastCellNum; j++) {
HSSFCell cell=row.getCell(j);
System.out.print(cell.getStringCellValue()+" ");
}
System.out.println();
}
// for (int i = 0; i < sheet.getLastRowNum(); i++) {
// for (int j = 0; j < sheet.getRow(i).getLastCellNum(); j++) {
// System.out.print(sheet.getRow(i).getCell(j).getStringCellValue()+" ");
// }
// System.out.println();
// } } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上一篇:[整理]Node入门 » 一本全面的Node.js教程 - Demo实践所遇到的问题


下一篇:HTML5中表单中新增加元素