需要引用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. LNMP集群架构篇

    一.LNMP介绍 1.使前端web服务和后端存储服务进行串联 2.主要实现处理php动态请求 工作原理: L:linux  N:nginx  M:mysql   P:php 二.lnmp部署 我的环境 ...

  2. alert(1) to win 7

    function escape(s) { // Pass inn "callback#userdata" var thing = s.split(/#/); if (!/^[a-z ...

  3. JavaScript 工作原理之十-使用 MutationObserver 监测 DOM 变化

    原文请查阅这里,略有删减,本文采用知识共享署名 4.0 国际许可协议共享,BY Troland. 本系列持续更新中,Github 地址请查阅这里. 这是 JavaScript 工作原理的第十章. 网络 ...

  4. windows下zookeeper集群安装

    windows下zookeeper单机版安装,见:https://www.cnblogs.com/lbky/p/9867899.html 一:zookeeper节点为什么是奇数个? 单机模式的zk进程 ...

  5. MJ瀑布流学习笔记

    1. 如果系统自带的布局的话,是这样: //系统自带的UICollectionViewFlowLayout 而不是UICollectionViewLayout UICollectionViewFlow ...

  6. C#中命名空间,C#程序中的一种代码组织形式,主要用来标识类的可以范围,引用using 命名空间

    C# C#中命名空间,C#程序中的一种代码组织形式,主要用来标识类的可以范围 use system; use system.collect.core; namespace sss{ } using 命 ...

  7. layer.js插件

    官方网址: http://layer.layui.com/

  8. Ponds

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  9. NOIp 数学知识点总结

    推荐阅读 NOIp 基础数论知识点总结: https://www.cnblogs.com/greyqz/p/number.html 排列组合 常用公式 排列:\[\displaystyle A_n^m ...

  10. 牛客网暑期ACM多校训练营(第五场) F - take —— 期望+树状数组+逆元

    看到一篇好的博客特意转出来观摩大佬:转:https://blog.csdn.net/greybtfly/article/details/81413526 题目大意:给n个箱子排成一排,从头到尾按顺序依 ...