需要引用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. python-验证功能的装饰器示例

    user_list=[ {'}, {'}, {'} ] current_dict={'username':None,'login':False} def auth(auth_type): def au ...

  2. 13DBUtils工具类

    如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC开发,本案例我们讲采用apache commons组件一个成员:DBUtils. DBUtils就是JDBC的简化开发工具包.需要项 ...

  3. scapy - 基于python的数据包操作库

    简介 地址:https://github.com/secdev/scapy scapy是一个基于python的交互式数据包操作程序和库. 它能够伪造或者解码多种协议的数据包,通过使用pcap文件对他们 ...

  4. Java反编译工具Luyten-0.5.3

    Luyten是一款很强大的反编译工具包,是一款github的开源工具,软件功能非常强大,界面简洁明晰.操作方便快捷,设计得很人性化. 工具软件下载路径:https://github.com/death ...

  5. Dubbo学习-2-注册中心搭建

    1.Dubbo支持如下几种注册中心,推荐使用zookeeper来作为注册中心. 2. 下载zookeeper https://zookeeper.apache.org/releases.html#do ...

  6. Django2 + ORM 做一个简单的登陆

    . ├── db.sqlite3 ├── manage.py ├── myormLogin │   ├── __init__.py │   ├── __pycache__ │   │   ├── __ ...

  7. 10:基于Tomcat部署Web工程

    1创建目录, 2.标识目录java文件,资源文件

  8. Harbor在安装前的几个注意点

    由于Harbor有1.8后和前的配置不一样,决定先安装1.8,结果报错如下 [root@localhost harbor]# ./install.sh [Step 0]: checking insta ...

  9. 使用随机森林实现OSM路网城市多车道信息提取

    Multilane roads extracted from the OpenStreetMap urban road network using random forests.,DOI:10.111 ...

  10. 整合ssm三大框架使用注解开发查询用户信息

    整合ssm三大框架使用注解开发查询用户信息 一.基础知识准备之spring mvc工作原理 二.分析 第一步:发起请求到前端控制器(DispatcherServlet) 第二步:前端控制器请求Hand ...