java对excel经行读写
package com.common.tool.util;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import javax.swing.plaf.synth.Region;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CreateExcel {
public static void main(String args[]) {
CreateExcel createExcel = new CreateExcel();
String path = "D:\\test.xlsx";
try {
createExcel.createExcel(path);
createExcel.getExcel(path);
createExcel.createExcelTop(path);
createExcel.getExcel(path);
} catch (Exception e) {
e.printStackTrace();
}
}
public void createExcel(String path) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("第一个sheet页");
HSSFRow row = sheet.createRow(0);
CellStyle cellStyle = wb.createCellStyle();
Cell cell = row.createCell(0);
cell.setCellValue(1);
FileOutputStream output = new FileOutputStream(path);
wb.write(output);
output.flush();
}
public void getExcel(String path) throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(path));
HSSFWorkbook wb = new HSSFWorkbook(fs);
int scount = wb.getNumberOfSheets();
System.out.println("sheet页的个数为:" + (scount));
for (int a = 0; a < scount; a++) {
String sheetName = wb.getSheetName(a);
System.out.println("第" + (a + 1) + "个sheet页的名字为" + sheetName + ",内容如下:");
HSSFSheet sheet = wb.getSheetAt(a);
HSSFSheet sheet1 = wb.getSheet("第一个sheet页");
String c = "";
int rcount = sheet.getLastRowNum();
System.out.println("第" + (a + 1) + "个sheet页有" + rcount + "行");
for (int i = 0; i <= rcount; i++) {
HSSFRow row = sheet.getRow(i);
if (null != row) {
int ccount = row.getLastCellNum();
System.out.println("第" + (i + 1) + "行有" + ccount + "个单元格");
for (int j = 0; j < ccount; j++) {
HSSFCell cell = row.getCell(j);
if (null != cell) {
int cellType = cell.getCellType();
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING:
c = cell.getStringCellValue();
if (c.trim().equals("") || c.trim().length() <= 0)
c = " ";
break;
case HSSFCell.CELL_TYPE_NUMERIC:
c = String.valueOf(cell.getNumericCellValue());
default:
break;
}
System.out.print("第" + (i + 1) + "行" + (j + 1) + "列的值为:" + c + " ");
} else {
System.out.print("第" + (i + 1) + "行" + (j + 1) + "列的值为:空" + " ");
}
}
System.out.println();
}else {
System.out.println("第" + (i + 1) + "行的值为:空");
}
}
}
}
public void createExcelTop (String path) throws Exception{
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont hssfFont = hssfWorkbook.createFont();
hssfFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
hssfFont.setColor(HSSFColor.RED.index);
style.setFont(hssfFont);
Sheet sheet = hssfWorkbook.createSheet();
Row row = sheet.createRow(1);
Cell cell = row.createCell(0);
cell.setCellValue("姓名");
row.createCell(1).setCellValue("年龄");
row.createCell(2).setCellValue("入学日期");
row.createCell(3).setCellValue("分数");
HSSFCellStyle style2 = hssfWorkbook.createCellStyle();
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setFillForegroundColor(HSSFColor.RED.index);
for (int i = 2;i<4;i++){
Row row2 = sheet.createRow(i);
row2.createCell(0).setCellValue("李明");
Cell cell2 = row2.createCell(1);
cell2.setCellValue("21");
cell2.setCellStyle(style2);
row2.createCell(2).setCellValue("2017年01月01日");
row2.createCell(3).setCellValue(78);
}
Row row5 = sheet.createRow(0);
Cell cell1 = row5.createCell(0);
cell1.setCellValue("学生成绩表");
cell1.setCellStyle(style);
sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
FileOutputStream output = new FileOutputStream(path);
hssfWorkbook.write(output);
output.flush();
}
}