标签:
准备:需要引用
poi-3.10-FINAL-sources.jar
poi-3.10-FINAL.jar
private void readExcel() throws IOException { FileInputStream stream = new FileInputStream("C:\\1.xls"); HSSFWorkbook workbook = new HSSFWorkbook(stream); HSSFSheet sheet = workbook.getSheetAt(0); // private static final String CELL_REFERENCE_FILE = "B6"; CellReference fileReference = new CellReference(CELL_REFERENCE_FILE); Row row; Cell cell; Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { row = rowIterator.next(); int currentRow = row.getRowNum(); if (currentRow == fileReference.getRow()) { cell = row.getCell(fileReference.getCol()); System.out.println("Data is " + cell.getStringCellValue()); } } stream.close(); }
private void writeExcel(ArrayList<String> list) throws Exception { String fileName="./"+projectName+"_File.xls"; File file = new File(fileName); if (!file.exists()) file.createNewFile(); FileOutputStream out = new FileOutputStream(file, false); CellStyle style = null; CellStyle style1 = null; HSSFWorkbook wb = new HSSFWorkbook(); Sheet sheet1 = wb.createSheet("TestResults"); ////////////////////////////////////////////////////////////////// style = wb.createCellStyle(); Font font=createFont(wb, "Calibri", false, (short) 11); style.setFont(font); style.setAlignment(CellStyle.ALIGN_LEFT); style.setWrapText(true); style1 = wb.createCellStyle(); HSSFColor color = wb.getCustomPalette().findColor((byte) 255, (byte) 255, (byte) 0); Font font1=createFont(wb, "Calibri", true, (short) 11); style1.setFont(font1); style1.setFillPattern((short) 1); style1.setAlignment(CellStyle.ALIGN_LEFT); style1.setWrapText(true); style1.setFillForegroundColor(color.getIndex()); for (int i = 0; i < list.size(); i++) { Row rows = sheet1.createRow(i + 1); Cell cell = rows.createCell(0); if(list.get(i).endsWith(".c")||list.get(i).endsWith(".h")) { cell.setCellStyle(style); }else { cell.setCellStyle(style1); } cell.setCellValue(list.get(i)); } sheet1.setColumnWidth(0, 80 * 256); sheet1.setColumnWidth(1, 40 * 256); wb.write(out); out.close(); } private Font createFont(HSSFWorkbook wb, String fontName, boolean isBold, short height) { Font font = null; font = wb.createFont(); font.setFontName(fontName); if (isBold) { font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); } font.setFontHeightInPoints(height); return font; }
标签:
原文地址:http://blog.csdn.net/tiankefeng19850520/article/details/45274081