需要引用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();

}

}

POI之Excel文档增删改查的更多相关文章

  1. Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查

    今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...

  2. dom4j解析xml文档(增删改查)

    package itcast.dom4j; import java.io.File; import java.io.FileOutputStream; import java.io.FileWrite ...

  3. struts2中利用POI导出Excel文档并下载

    1.项目组负责人让我实现这个接口,因为以前做过类似的,中间并没有遇到什么太困难的事情.其他不说,先上代码: package com.tydic.eshop.action.feedback; impor ...

  4. Java之Poi导出Excel文档

    一.Poi简介 在后台管理系统中,我们经常要做的导出操作,通常导出为Excel文档的形式,而Poi则提供了这种需要的支持. 二.Workbook/HSSFWorkbook/XSSFWorkbook 1 ...

  5. POI导出Excel文档通用工具方法

    import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Map; imp ...

  6. 模板页显示Excel数据Gridview增删改查

    <%@ Page Title="主页" Language="C#" MasterPageFile="~/Site.master" Au ...

  7. POI 读取Excel文档中的数据——兼容Excel2003和Excel2007

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. HSSF - 提供读写Microsoft Exce ...

  8. 使用struts2和poi导出excel文档

    poi眼下应该是比較流行的操作excel的工具了.这几天做了个struts2和poi结合使用来实现导出excel的功能.个人认为还是比較有用的.代码阅读起来也非常easy.下来就来分享下我的心得 1  ...

  9. 【Java】常用POI生成Excel文档设置打印样式

    package poi_test; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi ...

随机推荐

  1. Tensorflow学习笔记3:卷积神经网络实现手写字符识别

    # -*- coding:utf-8 -*- import tensorflow as tf from tensorflow.examples.tutorials.mnist import input ...

  2. Linux性能优化从入门到实战:09 内存篇:Buffer和Cache

      Buffer 是缓冲区,而 Cache 是缓存,两者都是数据在内存中的临时存储.   避免跟文中的"缓存"一词混淆,而文中的"缓存",则通指内存中的临时存储 ...

  3. windows重装系统之前与之后进行的操作

    1.原系统的备份 避免重装遇到故障无法恢复,给自己留一条后路. 重装系统之前首先进行一次系统备份,我使用的备份软件是dism++,这个软件还可以完成其他的诸如空间回收.系统优化等操作: 软件地址:ht ...

  4. 新特性2-lambda表达式

    最近几天学习了一下lambda表达式,看了不少博客,感觉有一篇博客总结的一句话总结的很好:lambda表达式是一段可以传递的代码,它的核心思想是将面向对象中的传递数据变成传递行为.其实以前也有传递行为 ...

  5. 【LeetCode】二分 binary_search(共58题)

    [4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...

  6. WiFi密码新攻击破解方法,黑客攻破只需10秒

    近日,中国知名黑客安全组织东方联盟研究人员透露了一种新的WiFi黑客技术,使黑客更容易破解大多数现代路由器的WiFi密码,并且攻破只需要10秒,速度非常快. 方法是利用由流行的密码破解工具Hashca ...

  7. 02.list--约瑟夫环

    from fib import fib # 参考01.线性表 def josephus_a(n, k, m): """ 约瑟夫环 没有人用0表示,n个人出列即结束 :pa ...

  8. macOS BLAS LAPACK

    /System/Library/Frameworks/Accelerate.framework/Frameworks/vecLib.framework/Headers

  9. Conservation Vs Non-conservation Forms of conservation Equations

    What does it mean? The reason they are conservative or non-conservative has to do with the splitting ...

  10. GEI步态能量图生成

    步态能量图生成主要有两步,主要为: 在原始轮廓图上对人的轮廓进行裁剪,在下面制作步态能量图图片叠加以什么为中心位置也是一个问题.一般有两种方式,一种是中心位置为人体宽的一半.另一种是以头顶为中心位置. ...