需要引用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. leetcode69. x 的平方根 🌟

    题目: 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 ...

  2. 【转】C/C++产生随机数

    转自:https://www.cnblogs.com/vectors07/p/8185215.html C/C++怎样产生随机数:这里要用到的是rand()函数, srand()函数,C语言/C++里 ...

  3. ltp-ddt eth_parallel_processing

    ETH_S_FUNC_PARALLEL_PROCESSING: source 'common.sh'; prepare_nfs_mount.sh "/mnt/nfs_mount"| ...

  4. layui树形表格支持非异步和异步加载

    layui树形表格支持非异步和异步加载. 仓库地址:https://gitee.com/uniqid/ 使用示例如下: <div class="uui-admin-common-bod ...

  5. flask之路径与函数的映射

    一:运行报错 OSError: [Errno 98] Address already in use:5000端口可能被占 lsof -i:端口号  查看端口被那个进程使用,结果是python3.5 k ...

  6. LeetCode--058--最后一个单词(java)

    给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: &quo ...

  7. Oracle12c创建及删除PDB

    在12C R1版本中只支持Global Shared Undo模式,所有container共享一个UNDO表空间:在12C R2引入了PDB Local Undo模式,每个container都有自己的 ...

  8. 英语单词vendors

    vendors 来源——https://www.docker.com/products/docker-hub Share and Collaborate with Docker Hub Docker ...

  9. 【CF1243B2】Character Swap (Hard Version)【思维】

    题意:给定两个字符串,问是否存在交换方案使得两个字符串相同,方案为交换次数小于等于2n,且每次只交换s1与s2中的一个字符 题解:考虑从前往后枚举,当第i位不同时,考虑找后边的第j位,若存在这样的第j ...

  10. js点击获取—通过JS获取图片的绝对对坐标位置

    一.通过JS获取鼠标点击时图片的相对坐标位置 源代码如下所示:  <!DOCTYPE html> <html lang="en"> <head> ...