• POI之Excel文档增删改查


    需要引用apache第三方lib库poi

    支持xls、xlsx格式excel读写操作

    package com.hua.excel;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.Iterator;

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.formula.functions.Column;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    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.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class WorkExcel implements Excel {

    private boolean isXls=false;
    private Sheet sheet;
    private Workbook wb;
    private String savePath;

    public WorkExcel(String excelPath,String sheetname) throws IOException{
    savePath=excelPath;
    isXls=excelPath.endsWith(".xls");
    FileInputStream excelfile = null;
    File file = new File (excelPath);
    if(file.exists()){
    excelfile=new FileInputStream(excelPath);
    }
    if(!isXls){
    // wb = new XSSFWorkbook(excelfile);
    // setActiveSheet(sheetname);
    if(null!=excelfile){
    wb =new XSSFWorkbook(excelfile);
    setActiveSheet(sheetname);
    } else {
    wb =new XSSFWorkbook();
    createSheet(sheetname);
    }
    } else {
    // wb = new HSSFWorkbook(excelfile);
    // setActiveSheet(sheetname);
    if(null!=excelfile){
    wb =new HSSFWorkbook(excelfile);
    setActiveSheet(sheetname);
    } else {
    wb =new HSSFWorkbook();
    createSheet(sheetname);
    }

    }
    }

    @Override
    public void insertColumn(int columnIndex) {
    if(isXls){
    int maxCellNum=columnIndex;
    for(Iterator<Row> rowIterator=sheet.rowIterator();rowIterator.hasNext();){
    HSSFRow row = (HSSFRow) rowIterator.next();
    for(int i=row.getLastCellNum()-1;i>=columnIndex;i--){
    HSSFCell cell2 = row.getCell(i);
    if(cell2==null){
    continue;
    }
    row.moveCell(cell2, (short)(i+1));
    }
    HSSFCell cell = row.getCell(columnIndex);
    if(cell!=null){
    row.removeCell(cell);
    }
    if(maxCellNum<row.getLastCellNum()){
    maxCellNum=row.getLastCellNum();
    }
    }
    for(int n=maxCellNum-2;n>columnIndex;n--){
    int width=sheet.getColumnWidth(n-1);
    sheet.setColumnWidth(n, width);

    }
    } else {
    int maxCellNum=columnIndex;
    for(Iterator<Row> rowIterator=sheet.rowIterator();rowIterator.hasNext();){
    XSSFRow row = (XSSFRow)rowIterator.next();
    for(int i=row.getLastCellNum()-1;i>=columnIndex;i--){
    Cell cell2 = row.getCell(i);
    if (cell2==null) {
    continue;
    }
    String value = getCellvalue(row.getRowNum(), i);
    setCellvalue(row.getRowNum(), i+1, value);
    setCellStyle(row.getRowNum(), i+1, cell2.getCellStyle());
    row.removeCell(cell2);
    }
    if(maxCellNum<row.getLastCellNum()){
    maxCellNum=row.getLastCellNum();
    }
    }
    for(int n=maxCellNum-2;n>columnIndex;n--){
    int width = sheet.getColumnWidth(n-1);
    sheet.setColumnWidth(n, width);

    }
    }
    }

    @Override
    public void insertRow(int rowIndex) {
    // TODO 自动生成的方法存根
    sheet.shiftRows(rowIndex, sheet.getLastRowNum(), 1,true,false);
    sheet.createRow(rowIndex);
    }

    @Override
    public void createSheet(String sheetName) {
    // TODO 自动生成的方法存根
    sheet = wb.createSheet(sheetName);
    }

    @Override
    public void delColumn(int columnIndex) {
    if(isXls){
    int maxCellNum = columnIndex;
    for(Iterator<Row> rowiIterator = sheet.rowIterator();rowiIterator.hasNext();){
    HSSFRow row = (HSSFRow) rowiIterator.next();
    HSSFCell cell = row.getCell(columnIndex);
    if(cell!=null){
    row.removeCell(cell);
    }

    for(int i=columnIndex;i<row.getLastCellNum();i++){
    HSSFCell cell2= row.getCell(i+1);
    if(cell2==null){
    continue;
    }
    row.moveCell(cell2, (short)i);

    }
    if(maxCellNum<row.getLastCellNum()){
    maxCellNum=row.getLastCellNum();
    }
    }
    for(int n= columnIndex;n<maxCellNum;n++){
    int width=sheet.getColumnWidth(n+1);
    sheet.setColumnWidth(n, width);
    }
    } else {
    int maxCellNum = columnIndex;
    for(Iterator<Row> rowiIterator = sheet.rowIterator();rowiIterator.hasNext();){
    XSSFRow row = (XSSFRow) rowiIterator.next();
    Cell cell = row.getCell(columnIndex);
    if(cell!=null){
    row.removeCell(cell);
    }

    for(int i=columnIndex;i<row.getLastCellNum();i++){
    Cell cell2= row.getCell(i+1);
    if(cell2==null){
    continue;
    }
    String value = getCellvalue(row.getRowNum(), i+1);
    setCellvalue(row.getRowNum(), i,value);
    setCellStyle(row.getRowNum(), i, cell2.getCellStyle());
    row.removeCell(cell2);
    }
    if(maxCellNum<row.getLastCellNum()){
    maxCellNum=row.getLastCellNum();
    }
    }
    for(int n = columnIndex;n<maxCellNum;n++){
    int width = sheet.getColumnWidth(n+1);
    sheet.setColumnWidth(n, width);
    }
    }
    }

    @Override
    public void delRow(int rowIndex) {
    Row row = sheet.getRow(rowIndex);
    sheet.removeRow(row);
    sheet.shiftRows(rowIndex+1, sheet.getLastRowNum(), -1, true, false);
    }

    @Override
    public String getCellvalue(int rowIndex, int columnIndex) {
    Row row = sheet.getRow(rowIndex);
    if(row==null){
    return "";
    }
    Cell cell = row.getCell(columnIndex);
    if(cell==null){
    return "";
    }
    cell.setCellType(Cell.CELL_TYPE_STRING);

    return cell.getStringCellValue();
    }

    @Override
    public int getColumnNumb(int rowIndex) {
    // TODO 自动生成的方法存根
    return sheet.getRow(rowIndex).getLastCellNum();
    }
    /**
    * 获取最后行索引
    */
    @Override
    public int getLastRowIndex() {

    return sheet.getLastRowNum();
    }

    /**
    * 按照行读取内容
    */
    @Override
    public ArrayList<String> getRow(int rowIndex) {
    ArrayList<String> al = new ArrayList<String>();
    Row row = sheet.getRow(rowIndex);
    for(int i=0;i<row.getLastCellNum();i++){
    al.add(getCellvalue(rowIndex, i));
    }
    return al;
    }

    @Override
    public void setRow(int rowIndex,ArrayList<String> al) {
    for(int i=0;i<al.size();i++){
    setCellvalue(rowIndex, i, al.get(i));
    }
    }

    @Override
    public void saveExcel() throws IOException {
    // TODO 自动生成的方法存根
    OutputStream stream = new FileOutputStream(savePath);
    wb.write(stream);
    stream.close();
    wb.close();
    }

    @Override
    public void setActiveSheet(int sheetIndex) {
    sheet=wb.getSheetAt(sheetIndex);
    if (sheet==null) {
    sheet=wb.createSheet();
    }
    }

    @Override
    public void setActiveSheet(String sheetName) {
    sheet=wb.getSheet(sheetName);
    if (sheet==null) {
    sheet=wb.createSheet(sheetName);
    }
    }

    @Override
    public void setCellStyle(int rowIndex, int columnIndex, CellStyle cellStyle) {
    // TODO 自动生成的方法存根
    Row row =sheet.getRow(rowIndex);
    if(row==null){
    return ;
    }
    Cell cell =row.getCell(columnIndex);
    if(cell==null){
    return ;
    }
    cell.setCellStyle(cellStyle);
    }

    @Override
    public void setCellvalue(int rowIndex, int columnIndex,String value) {
    Row row =sheet.getRow(rowIndex);
    if(row==null){
    row=sheet.createRow(rowIndex);
    }
    Cell cell =row.createCell(columnIndex);
    cell.setCellType(Cell.CELL_TYPE_STRING);
    cell.setCellValue(value);
    }
    public static void main(String[] args) throws IOException {
    WorkExcel we = new WorkExcel("d:/java/exceltest.xls", "Test1");
    // we.delColumn(4);
    // we.delRow(1);
    we.setCellvalue(1, 1, "2B");
    we.setCellvalue(2, 1, "3B");
    we.setActiveSheet("Test2");
    we.setCellvalue(1, 1, "2B2");
    we.setCellvalue(2, 1, "3B2");
    we.saveExcel();

    }

    }

  • 相关阅读:
    Apache2支持py,pl,cgi,sh
    javascript利用Active操作本地文件
    桥接模式下的手机-电脑-开发板连接
    Django signal
    mysql改变表的编码字符集(utf8改为utf8mb4)
    Mysql 多表关联查询
    Python unicode转义字符u的处理
    How to filter a DateTimeField by date in Django?
    python闭包变量迟邦定
    Ubuntu16.04安装&创建Python虚拟环境
  • 原文地址:https://www.cnblogs.com/hua198/p/5716897.html
Copyright © 2020-2023  润新知