Exemple : http://xiazai.VeVB.COm/201407/tools/java-excel-dependency(VeVB.COm).rar
Utilisation d'ExcelUtil.java源码:
package com.itjh.javaUtil; importer java.io.File; importer java.io.FileInputStream; importer java.io.FileNotFoundException; importer java.io.FileOutputStream; importer java.io.IOException; importer java.io.OutputStream; importer java.text.DecimalFormat; importer java.util.LinkedList; importer java.util.List; importer javax.servlet.http.HttpServletResponse; importer org.apache.poi.hssf.usermodel.HSSFCell; importer org.apache.poi.hssf.usermodel.HSSFRichTextString; importer org.apache.poi.hssf.usermodel.HSSFRow; importer org.apache.poi.hssf.usermodel.HSSFSheet; importer org.apache.poi.hssf.usermodel.HSSFWorkbook; importer org.apache.poi.openxml4j.exceptions.InvalidFormatException; importer org.apache.poi.ss.usermodel.Cell ;importer org.apache.poi.ss.usermodel.DateUtil;importer org.apache.poi.ss.usermodel.Row; importer org.apache.poi.ss.usermodel.Sheet; importer org.apache.poi.ss.usermodel.Workbook; importer org.apache.poi.ss.usermodel.WorkbookFactory ;/** * Pour Excel, pour Office 2007, Excel et Office 2007.<br/> *依赖于poi-3.9-20121203.jar,poi-ooxml-3.9-20121203.jar,poi-ooxml-schemas-3.9- * 20121203.jar,dom4j-1.6.1.jar<br/> *有参构造函数参数为excel的全路径<br/> * * @author 宋立君 * @date 2014年07月03日 */public class ExcelUtil {// excel文件路径private String path = "";// Il s'agit d'un fichier Excel.private boolean autoColumnWidth = false;/** * Il n'est pas nécessaire de le faire */public ExcelUtil() {}/** *有参构造函数 * * @param path * excel路径 */public ExcelUtil(String path) {this.path = path;}/** * 读取某个工作簿上的所有单元格的值。 * * @param feuilleCommander * * @throws IOException * @return List<Object[]> 所有单元格的值。 * @throws IOException * @throws FileNotFoundException * excel文件没有找到异常。 * @throws InvalidFormatException * @author 宋立君 * @date 2014年07月03日 */public List<Object[]> read(int sheetOrder) throws FileNotFoundException,IOException, InvalidFormatException {FileInputStream fis = new FileInputStream(path);Workbook workbook = WorkbookFactory.create(fis);if (fis != null) {fis.close();}Sheet sheet = workbook.getSheetAt(sheetOrder);// []> valueList = new LinkedList<Object[]>();// for (Row row : sheet) {// il s'agit d'Object[] rowObject = null;for (Cell cell : row) {// cell.getCellType pour cell. (cell.getCellType()) {case Cell.CELL_TYPE_BOOLEAN:// Boolean对象的方法rowObject = CollectionUtil.addObjectToArray(rowObject,cell.getBooleanCellValue());break;case Cell.CELL_TYPE_NUMERIC:// si (DateUtil.isCellDateFormatted(cell)) {// 读取日期格式rowObject = CollectionUtil.addObjectToArray(rowObject,cell.getDateCellValue());} else {DecimalFormat df = new DecimalFormat();// 单元格的值,替换掉,Valeur de chaîne = df.format(cell.getNumericCellValue()).replace(",", "");// 读取数字rowObject = CollectionUtil.addObjectToArray(rowObject,value);}break;case Cell.CELL_TYPE_FORMULA:// 读取公式rowObject = CollectionUtil.addObjectToArray(rowObject,cell.getCellFormula());break;case Cell.CELL_TYPE_STRING:// StringrowObject = CollectionUtil.addObjectToArray(rowObject, cell.getRichStringCellValue().toString());break;}}// return valueList.valueList.add(rowObject);}return valueList ;}/** * * * @param sheetOrder * 工作簿序号,从0开始。 * @param colum * 列数 从1开始 * @param row *行数 从1开始 * @return 单元格的值。 * @throws Exception * 加载excel异常。 * @author 宋立君 * @date 2014年07月03日 */public String read(int sheetOrder, int colum, int row) lève une exception {FileInputStream fis = new FileInputStream(path);Workbook workbook = WorkbookFactory.create(fis);if (fis != null) {fis.close();}Feuille = workbook.getSheetAt(sheetOrder);Row rows = sheet.getRow(ligne - 1);Cell cell = rows.getCell(colum - 1);String content = cell.getStringCellValue();return content;}/** * 在指定的工作簿、行、列书写值。 * * @param sheetOrder * 工作簿序号,基于0. * @param colum * 列 基于1 * @param row * 行 基于1 * @param content * 将要被书写的内容。 * @throws Exception * 书写后保存异常。 * @author 宋立君 * @date 03 juillet 2014 */public void write(int sheetOrder, int colum, int row, String content)lance une exception {FileInputStream fis = new FileInputStream(path);Workbook workbook = WorkbookFactory.create(fis);if (fis ! = null) {fis.close();}Feuille = workbook.getSheetAt(sheetOrder);Row rows = sheet.createRow(row - 1);Cell cell = rows.createCell(colum - 1);cell.setCellValue(content);FileOutputStream fileOut = new FileOutputStream(path);workbook.write(fileOut);fileOut.close( );}/** * * * @param sheetOrder * 工作区序号* @return int IOException * @throws IOException * Excel Excel 异常。 * @throws InvalidFormatException * @author IOException * @date 2014年07月03日 */public int getSheetLastRowNum(int sheetOrder) jette IOException, InvalidFormatException {FileInputStream fis = new FileInputStream(path);Workbook workbook = WorkbookFactory.create(fis);if (fis != null) {fis.close();}Feuille = workbook.getSheetAt(sheetOrder);return feuille.getLastRowNum();}/** * Fichier Excel, fichier chemin d'accès * * @param sheetName * fichier feuille * @param fieldName * fichier * @param data * fichier * @throws IOException * @author 宋立君 * @date 2014年07月03日 */public void makeExcel(String sheetName, String[] fieldName,List<Object[]> data) throws IOException {// 在内存中生成工作薄HSSFWorkbook classeur = makeWorkBook(sheetName, fieldName, data);// cheminfichier);// System.out.println(chemin+"----------"+file.exists());if (!file.exists())file.mkdirs();FileOutputStream fileOut = new FileOutputStream(chemin );workbook.write(fileOut);fileOut.close();}/** * Fichier Excel。 * * @param excelName * Fichier Excel * @param sheetName * Fichier feuille * @param fieldName * Paramètres * @param data * Paramètres * @param réponse * réponse * @throws IOException *转换流时IO错误 * @author 宋立君 * @date 2014年07月03日 */public void makeStreamExcel (String excelName, String sheetName, String[] fieldName, List<Object[]> data, HttpServletResponse réponse) lance IOException {OutputStream os = null;response.reset(); // Fonctionnement os = réponse.getOutputStream(); // Demande de réponseresponse.setHeader("Content-disposition", "attachment; filename="+ new String(excelName.getBytes(), "ISO-8859-1")); // Réponse à la questionresponse.setContentType("application/msexcel"); // Fichier de travail HSSFWorkbook = makeWorkBook(sheetName, fieldName, data);os.flush();workbook.write(os);}/** * * * @param sheetName * 工作表对象名称 * @param fieldName * 首列列名称 * @param data * 数据 * @return HSSFWorkbook * @author 宋立君 * @date 2014年07月03日 */private HSSFWorkbook makeWorkBook(String sheetName, String[] fieldName,List<Object[]> data) {// Integer collength[] = new Integer[fieldName.length];// 产生工作薄对象HSSFWorkbook workbook = new HSSFWorkbook();// Feuille HSSFSheet = workbook.createSheet();// Feuille de travail HSSFSheet = workbook.createSheet();// UTF_16workbook.setSheetName(0, sheetName);//产生一行HSSFRow row = sheet.createRow(0);// 产生单元格HSSFCell cell;// 写入各个字段的名称for (int i = 0; i < fieldName.length; i++) {// Cell = row.createCell((short) i);//设置单元格内容为字符串型cell.setCellType(HSSFCell.CELL_TYPE_STRING);//为了能在单元格中输入中文,设置字符集为UTF_16// cell.setEncoding(HSSFCell.ENCODING_UTF_16);// Il s'agit d'une cellule.setCellValue(new HSSFRichTextString(fieldName[i]));// d'une valeur de collength[i] = fieldName[i].getBytes().length;}// d'une autre chaîne tempCellContent = "";// 中的一行for (int i = 0; je < data.size(); i++) {Object[] tmp = data.get(i);// 生成一行row = sheet.createRow(i + 1);for (int j = 0; j < tmp.length; j++) {cell = row. createCell((court) j);// Vous avez besoin de Stringcell.setCellType(HSSFCell.CELL_TYPE_STRING);tempCellContent = (tmp[j] == null) ? "" : tmp[j].toString();cell.setCellValue(new HSSFRichTextString(tempCellContent));//标题列数小于数据列数时。collength = CollectionUtil.addObjectToArray(collength,tempCellContent.getBytes().length);} else {// Si (collength[j] < tempCellContent.getBytes().length) {collength[j] = tempCellContent.getBytes().length;}}}}}// 自动调整列宽度。if (autoColumnWidth) {// 调整列为这列文字对应的最大宽度。for (int i = 0; i < fieldName.length; i++) {sheet.setColumnWidth(i, collength[i] * 2 * 256);}}return workbook;}/** * * @param autoColumnWidth * true ou false */public void setAutoColumnWidth(boolean autoColumnWidth) {this.autoColumnWidth = autoColumnWidth;}}