博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读写EXCEL
阅读量:4213 次
发布时间:2019-05-26

本文共 2386 字,大约阅读时间需要 7 分钟。



准备:需要引用

poi-3.10-FINAL-sources.jar

poi-3.10-FINAL.jar

 

读取EXCEL

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
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(); }

EXCEL

private void writeExcel(ArrayList
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://ycumi.baihongyu.com/

你可能感兴趣的文章
MSM8X60 USB控制器流程分析
查看>>
sd卡驱动分析之card
查看>>
sd卡驱动分析之core
查看>>
sd卡驱动分析之host
查看>>
sd卡驱动分析之相关硬件操作和总结
查看>>
好的播文
查看>>
linux dd命令解析
查看>>
linux find命令详解
查看>>
S3C2440上touchscreen触摸屏驱动
查看>>
ARM-Linux驱动-触摸屏驱动分析
查看>>
GPIO的上拉电阻的作用
查看>>
kernel power off流程分析
查看>>
Qualcomm pmic充电流程分析(msm8660)
查看>>
web开发了解
查看>>
android switch模块
查看>>
linux内核中container_of
查看>>
USB History Viewing
查看>>
Android 关机流程分析-案例高通平台
查看>>
android restart reason机制
查看>>
linux cpufeq相关知识
查看>>