Java实现读取及生成Excel文件的方法
本文实例讲述了Java实现读取及生成Excel文件的方法。分享给大家供大家参考,具体如下:
一、读取Excel文件
需要先下载poi-3.0.1-FINAL-20070705.jar(点击此处本站下载poi-3.0.1-FINAL-20070705.jar。)
ExcelExamRead.java
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
publicclassExcelExamRead{
/**读Excel文件内容*/
publicvoidshowExcel(StringexcelName){
Filefile=newFile(excelName);
FileInputStreamin=null;
try{
//创建对Excel工作簿文件的引用
in=newFileInputStream(file);
HSSFWorkbookhwb=newHSSFWorkbook(in);
HSSFSheetsheet=hwb.getSheet("myFirstExcel");//根据指定的名字来引用此Excel中的有效工作表
//读取Excel工作表的数据
System.out.println("下面是Excel文件"+file.getAbsolutePath()+"的内容:");
HSSFRowrow=null;
HSSFCellcell=null;
introwNum=0;
//行标
intcolNum=0;
//列标
for(;rowNum<9;rowNum++){
//获取第rowNum行
row=sheet.getRow((short)rowNum);
for(colNum=0;colNum<5;colNum++){
cell=row.getCell((short)colNum);//根据当前行的位置来创建一个单元格对象
System.out.print(cell.getStringCellValue()+"\t");//获取当前单元格中的内容
}
System.out.println();//换行
}
in.close();
}catch(Exceptione){
System.out
.println("读取Excel文件"+file.getAbsolutePath()+"失败:"+e);
}finally{
if(in!=null){
try{
in.close();
}catch(IOExceptione1){
}
}
}
}
publicstaticvoidmain(String[]args){
ExcelExamReadexcel=newExcelExamRead();
StringexcelName="D:/ExcelExamRead.xls";
excel.showExcel(excelName);
}
}
二、生成Excel文件
ExcelExamWrite.java:
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFCellStyle;
importorg.apache.poi.hssf.usermodel.HSSFFont;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.hssf.util.Region;
//创建Excel文件
publicclassExcelExamWrite{
//新建一个Excel文件,里面添加5行5列的内容,另外添加一个合并2行5列的大单元格以及一个合并2行1列的5个合并单元格。
publicvoidcreateExcel(StringfileName){
Filefile=newFile(fileName);//创建excel文件对象
FileOutputStreamfOut=null;
try{
//创建一个新的HSSFWorkbook对象
HSSFWorkbookworkbook=newHSSFWorkbook();
//创建一个Excel的工作表,可以指定工作表的名字
HSSFSheetsheet=workbook.createSheet("myFirstExcel");
//创建字体,红色、粗体
HSSFFontfont=workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFFontfont1=workbook.createFont();
//创建字体,黑色、非粗体
font1.setColor(HSSFFont.COLOR_NORMAL);
font1.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
//创建单元格的格式,如居中、左对齐等
HSSFCellStylecellStyle=workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平方向上居中对齐
//垂直方向上居中对齐
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellStyle.setFont(font);//设置字体
HSSFCellStylecellStyle1=workbook.createCellStyle();
cellStyle1.setAlignment(HSSFCellStyle.ALIGN_LEFT);
cellStyle1.setFont(font1);
//下面将建立一个4行3列的表。第一行为表头。
introwNum=0;//行标
intcolNum=0;//列标
//建立表头信息
HSSFRowrow=sheet.createRow((short)rowNum);//在索引0的位置创建行
HSSFCellcell=null;//单元格
for(colNum=0;colNum<5;colNum++){
//在当前行的colNum列上创建单元格
cell=row.createCell((short)colNum);
//定义单元格为字符类型,也可以指定为日期类型、数字类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//定义编码方式,为了支持中文,这里使用了ENCODING_UTF_16
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);//为单元格设置格式
cell.setCellValue("表头-第"+(colNum+1)+"列");//添加内容至单元格
}
rowNum++;
for(;rowNum<5;rowNum++){
//新建第rowNum行
row=sheet.createRow((short)rowNum);
for(colNum=0;colNum<5;colNum++){
//在当前行的colNum位置创建单元格
cell=row.createCell((short)colNum);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle1);
cell.setCellValue("表体-第"+rowNum+"行第"+(colNum+1)
+"列");
}
}
//合并单元格
//先创建2行5列的单元格,然后将这些单元格合并为2个大单元格
rowNum=5;
for(;rowNum<9;rowNum++){
row=sheet.createRow((short)rowNum);
for(colNum=0;colNum<5;colNum++){
//在当前行的colNum位置创建单元格
cell=row.createCell((short)colNum);
}
}
//建立第一个大单元格,高度为2,宽度为2
rowNum=5;
colNum=0;
Regionregion=newRegion(rowNum,(short)colNum,(rowNum+1),
(short)(colNum+4));
sheet.addMergedRegion(region);
//获得第一个大单元格
cell=sheet.getRow(rowNum).getCell((short)colNum);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellValue("合并行单元格");
//建立第二个大单元格,高度为2,宽度为3
rowNum=7;
for(colNum=0;colNum<5;colNum++){
region=newRegion(rowNum,(short)colNum,(rowNum+1),
(short)(colNum));
sheet.addMergedRegion(region);
//获得第二个大单元格
cell=sheet.getRow(rowNum).getCell((short)colNum);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(cellStyle);
cell.setCellValue("合并列单元格");
}
//新建一输出文件流
fOut=newFileOutputStream(file);
//将创建的内容写到指定的Excel文件中
workbook.write(fOut);
fOut.flush();
fOut.close();//操作结束,关闭文件
System.out.println("Excel文件创建成功!\nExcel文件的存放路径为:"
+file.getAbsolutePath());
}catch(Exceptione){
System.out.println("Excel文件"+file.getAbsolutePath()
+"创建失败\n其原因为:"+e);
}finally{
if(fOut!=null){
try{
fOut.close();
}catch(IOExceptione1){
}
}
}
}
publicstaticvoidmain(String[]args)throwsException{
ExcelExamWriteexcel=newExcelExamWrite();
StringfileName="e:/ExcelExamWrite.xls";
excel.createExcel(fileName);
}
}
更多关于java相关内容感兴趣的读者可查看本站专题:《Java操作Excel技巧总结》、《Java+MySQL数据库程序设计总结》、《Java数据结构与算法教程》、《Java文件与目录操作技巧汇总》及《Java操作DOM节点技巧总结》
希望本文所述对大家java程序设计有所帮助。