XlsOrXlsxUtil
两种读写 Xls Or Xlsx 的方式:
1.方式一:
package com.ting.xls;import com.google.common.collect.Maps;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class XlsOrXlsxUtil {public static void main(String[] args) throws Exception {String filepath = "/ting/xls/Echo.xls";String filepath2 = "/ting/xls/re5.xls";// List list = readExcelWithTitle(filepath);List list = readExcelWithoutTitle(filepath);Map map = Maps.newHashMapWithExpectedSize(1);map.put("re", list.get(0));writeExcel(new FileOutputStream(new File(filepath2)), "xls", map);}//这应该是一个比较全的示例了,更加复杂的功能可以在此基础上扩展。此示例基于apache的POI类库,相关jar包就不列举了。这个类库很通用,网上很好找。//1、不包含单元格合并的写excel/*** excel导出到输出流* * 谁调用谁负责关闭输出流** @param os 输出流* @param excelExtName excel文件的扩展名,支持xls和xlsx,不带点号* @param data* @throws IOException*/public static void writeExcel(OutputStream os, String excelExtName, Map data) throws IOException {Workbook wb = null;try {if ("xls".equals(excelExtName)) {wb = new HSSFWorkbook();} else if ("xlsx".equals(excelExtName)) {wb = new XSSFWorkbook();} else {throw new Exception("当前文件不是excel文件");}for (String sheetName : data.keySet()) {Sheet sheet = wb.createSheet(sheetName);List rowList = data.get(sheetName);for (int i = 0; i < rowList.size(); i++) {List cellList = (List) rowList.get(i);Row row = sheet.createRow(i);for (int j = 0; j < cellList.size(); j++) {Cell cell = row.createCell(j);cell.setCellValue((String) cellList.get(j));}}}wb.write(os);} catch (Exception e) {e.printStackTrace();} finally {if (wb != null) {wb.close();}}}//2、包含单元格合并的写excel 辅助vo// class ExcelData {
// private String value;//单元格的值
//
// private int colSpan = 1;//单元格跨几列
//
// private int rowSpan = 1;//单元格跨几行
//
// private boolean alignCenter;//单元格是否居中,默认不居中,如果选择是,则水平和上下都居中
//
// public boolean isAlignCenter() {
// return alignCenter;
//
// }
//
// public void setAlignCenter(boolean alignCenter) {
// this.alignCenter = alignCenter;
//
// }
//
// public String getValue() {
// return value;
//
// }
//
// public void setValue(String value) {
// this.value = value;
//
// }
//
// public int getColSpan() {
// return colSpan;
//
// }
//
// public void setColSpan(int colSpan) {
// this.colSpan = colSpan;
//
// }
//
// public int getRowSpan() {
// return rowSpan;
//
// }
//
// public void setRowSpan(int rowSpan) {
// this.rowSpan = rowSpan;
//
// }
//
// }//写excel文件的逻辑/*** excel导出到输出流* * 谁调用谁负责关闭输出流** @param os 输出流* @param excelExtName excel文件的扩展名,支持xls和xlsx,不带点号* @param data excel数据,map中的key是标签页的名称,value对应的list是标签页中的数据。list中的子list是标签页中的一行,子list中的对象是一个单元格的数据,包括是否居中、跨几行几列以及存的值是多少* @throws IOException*/// public static void testWrite(OutputStream os, String excelExtName, Map data) throws IOException {
// Workbook wb = null;
//
// CellStyle cellStyle = null;
//
// boolean isXls;
//
// try {
// if ("xls".equals(excelExtName)) {
// wb = new HSSFWorkbook();
//
// isXls = true;
//
// } else if ("xlsx".equals(excelExtName)) {wb = new XSSFWorkbook();
//
// isXls = false;
//
// } else {
// throw new Exception("当前文件不是excel文件");
//
// }
//
// cellStyle = wb.createCellStyle();
//
// if (isXls) {
// cellStyle.setAlignment(HorizontalAlignment.CENTER);
//
// cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//
// } else {
// cellStyle.setAlignment(HorizontalAlignment.CENTER);
//
// cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//
// }
//
// for (String sheetName : data.keySet()) {
// Sheet sheet = wb.createSheet(sheetName);
//
// List rowList = (List) data.get(sheetName);
//
i 代表第几行 从0开始
//
// for (int i = 0; i < rowList.size(); i++) {
// List cellList = (List) rowList.get(i);
//
// Row row = sheet.createRow(i);
//
// int j = 0;//j 代表第几列 从0开始
//
// for (ExcelData excelData : cellList) {
// if (excelData != null) {
// if (excelData.getColSpan() > 1 || excelData.getRowSpan() > 1) {
// CellRangeAddress cra = new CellRangeAddress(i, i + excelData.getRowSpan() - 1, j, j + excelData.getColSpan() - 1);
//
// sheet.addMergedRegion(cra);
//
// }
//
// Cell cell = row.createCell(j);
//
// cell.setCellValue(excelData.getValue());
//
// if (excelData.isAlignCenter()) {
// cell.setCellStyle(cellStyle);
//
// }
//
// j = j + excelData.getColSpan();
//
// } else {
// j++;
//
// }
//
// }
//
// }
//
// }
//
// wb.write(os);
//
// } catch (Exception e) {
// e.printStackTrace();
//
// } finally {
// if (wb != null) {
// wb.close();
//
// }
//
// }
//
// }//测试代码// public static void main(String[] args) throws IOException {
//
方式二:
package com.ting.xls;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;import java.io.FileInputStream;
import java.io.FileOutputStream;public class XlsOrXlsxWriteUtil {/*** excel导出到输出流* * 谁调用谁负责关闭输出流** @param os 输出流* @param excelExtName excel文件的扩展名,支持xls和xlsx,不带点号* // * @param data* @throws IOException*/public static void main(String[] args) throws Exception {String filepath = "/ting/xls/Echo4.xls";String filepath2 = "/ting/xls/Echo4.xls";FileInputStream fs = new FileInputStream(filepath);POIFSFileSystem ps = new POIFSFileSystem(fs);HSSFWorkbook wb = new HSSFWorkbook(ps);
// HSSFSheet sheet=wb.getSheetAt(0);
// HSSFRow row=sheet.getRow(0);
// System.out.println(sheet.getLastRowNum()+" "+row.getLastCellNum());FileOutputStream out = new FileOutputStream(filepath2);
// row=sheet.createRow((short)(sheet.getLastRowNum()+1));
// row.createCell(17).setCellValue(9.2);
// row.createCell(20).setCellValue(9);int sheetSize = wb.getNumberOfSheets();for (int i = 0; i < sheetSize; i++) {//遍历sheet页Sheet sheet = wb.getSheetAt(i);int rowSize = sheet.getLastRowNum() + 1;for (int j = 0; j < rowSize; j++) {//遍历行Row row = sheet.getRow(j);if (row == null) {//略过空行continue;}String u = null;Cell cell = row.getCell(5);int index = cell.getStringCellValue().indexOf("(LVEF");if (index == -1) {index = cell.getStringCellValue().indexOf("(EF");}if (index > 0) {try {int endIndex = cell.getStringCellValue().substring(index + 1, index + 20).indexOf(")");u = cell.getStringCellValue().substring(index + 1, index + 20).substring(0, endIndex);} catch (Exception e) {u = cell.getStringCellValue().substring(index, index);}
// System.out.printf(row.getCell(0).getStringCellValue());System.out.println(u);} else {u = "正常";
// System.out.printf(row.getCell(0).getStringCellValue());System.out.println(u);}int cellSize = row.getLastCellNum();//行中有多少个单元格,也就是有多少列Cell add_cell = row.createCell(cellSize);add_cell.setCellValue(u);}}out.flush();wb.write(out);out.close();
// System.out.println(row.getPhysicalNumberOfCells()+" "+row.getLastCellNum());}}
参考:
POI中如何能对已经存在的Excel2007文件新加或修改内容?-CSDN论坛
Java写xlsx_java读写excel(POI,支持xls和xlsx两种格式)(示例代码)_飞翔的牛蛙君的博客-CSDN博客
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
