POI读写excel文件

package com.newbee.brooder; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ReadOrWriteXLS { /** * @param args */ public static void main(String[] args) { writeToXLS(); readFromXLS(); } private static void readFromXLS() { FileInputStream inputStream = null; try { inputStream = new FileInputStream(System.getProperty("user.home") + File.separator + "test.xls"); HSSFWorkbook workbook = new HSSFWorkbook(inputStream); HSSFSheet sheet = workbook.getSheetAt(0); int rows = sheet.getPhysicalNumberOfRows();// 行数 for (int i = 0; i < rows; i++) { HSSFRow row = sheet.getRow(i); if (row!=null) { int cells = row.getLastCellNum(); String[] value = new String[cells]; for (int j = 0; j < cells; j++) { HSSFCell cell = row.getCell(j);// 单元格 if (cell != null) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA: break; case HSSFCell.CELL_TYPE_NUMERIC: value[j] += (long) cell.getNumericCellValue() + "/t"; break; case HSSFCell.CELL_TYPE_STRING: value[j] += cell.getStringCellValue() + "/t"; break; default: value[j] += "/t"; } if (value[j] != null) { value[j] = value[j].substring(4).trim(); // 前4个字符是null if (value[j].indexOf(" ") > 0) { value[j] = value[j].replaceAll(" ", ""); } } } } System.out.println(Arrays.toString(value)); } } } catch (FileNotFoundException e) { Logger.getLogger(ReadOrWriteXLS.class).debug(e); } catch (IOException e) { Logger.getLogger(ReadOrWriteXLS.class).debug(e); } finally { if (inputStream!=null) { try { inputStream.close(); inputStream = null; } catch (Exception e2) { Logger.getLogger(ReadOrWriteXLS.class).debug(e2); } } } } private static void writeToXLS() { ArrayList<String[]> content = new ArrayList<String[]>(); String[] s01 = "aa,bb,cc,dd,中文测试,ff".split(","); String[] s02 = "11,22,33,44,55,66,77".split(","); content.add(s01); content.add(s02); // 创建新的 Excel 工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 在 Excel 工作簿中建创一个工作表,其名为缺省值 sheet1 HSSFSheet sheet = workbook.createSheet("0"); // 创建字体 HSSFFont font = workbook.createFont(); // 把字体颜色设置为红色 font.setColor(HSSFFont.COLOR_NORMAL); // 把字体设置为粗体 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 创建格式 HSSFCellStyle cellStyle = workbook.createCellStyle(); // 把创建的字体付加于格式 cellStyle.setFont(font); int beginRow = 0; for (int i = 0; i < content.size(); i++) { // 在工作表中创建一行 HSSFRow row = sheet.createRow(beginRow++); int beginCol = 0; for (int j = 0; j < content.get(i).length; j++) { // 在一行中创建一个表格 HSSFCell cell = row.createCell((short) beginCol++); if (i == 0) { // 把上面的格式付加于一个单元格,第一行字体加粗 cell.setCellStyle(cellStyle); } // 设置此单元格中存入的是字符串 cell.setCellType(HSSFCell.CELL_TYPE_STRING); // 设置编码 这个是用来处理中文问题的 cell.setEncoding(HSSFCell.ENCODING_UTF_16); // 向此单元格中放入值 cell.setCellValue(new HSSFRichTextString(content.get(i)[j])); } } FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(System.getProperty("user.home")+File.separator+"test.xls"); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } catch (FileNotFoundException e) { Logger.getLogger(ReadOrWriteXLS.class).debug(e); } catch (IOException e) { Logger.getLogger(ReadOrWriteXLS.class).debug(e); } finally { if (outputStream!=null) { try { outputStream.close(); outputStream = null; } catch (IOException e) { Logger.getLogger(ReadOrWriteXLS.class).debug(e); } } } } }

你可能感兴趣的:(exception,工作,String,Excel,null,Class)