poi使用笔记

package com.feinno.report.utils.jxlsutil;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.feinno.report.constant.BillConstant;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

/**
*
*/
public static void insertFirstRows(String excelPath, String outPath) throws Exception {
XSSFWorkbook wb = null;
FileInputStream fis = null;
XSSFRow row = null;
File f = new File(excelPath);
try {
if (f != null) {
fis = new FileInputStream(f);
//获取已有的工作簿
wb = new XSSFWorkbook(fis);
//获取工作表名
XSSFSheet sheet1 = wb.getSheet("微信账单统计");
row = createRow(sheet1, 0, 1);
XSSFCell cell = row.createCell((short) 0);
row.createCell(0).setCellValue("#账号[" + BillConstant.WEI_XIN_BUSINESS_NUMBER + "]");
saveExcel(wb, outPath);
}
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

/**
*移除第一行
*/
public static void removeFirstRow(String excelPath, String outPath) throws Exception {
XSSFWorkbook wb = null;
FileInputStream fis = null;
XSSFRow row = null;
File f = new File(excelPath);
try {
if (f != null) {
fis = new FileInputStream(f);
//获取已有的工作簿
wb = new XSSFWorkbook(fis);
//获取工作表名
XSSFSheet sheet1 = wb.getSheet("微信账单统计");
sheet1.removeRow(sheet1.getRow(0));
// //移动单元格
// int lastRowNo = sheet1.getLastRowNum();
// sheet1.sh(1, lastRowNo, 1);
saveExcel(wb, outPath);
}
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* 找到需要插入的行数,并新建一个POI的row对象
*
* @param sheet
* @param rowIndex
* @return
*/
private static XSSFRow createRow(XSSFSheet sheet, Integer rowIndex, Integer size) {
XSSFRow row = null;
if (sheet.getRow(rowIndex) != null) {
////总行数
int lastRowNo = sheet.getLastRowNum();
sheet.shiftRows(rowIndex, lastRowNo, size);//把rowIndex-lastRowNo向下移动size行
}
row = sheet.createRow(rowIndex);
return row;
}

/**
* 保存工作薄
*
* @param wb
*/
public static void saveExcel(XSSFWorkbook wb, String outPath) {
FileOutputStream fileOut;
try {
fileOut = new FileOutputStream(outPath);
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}



//EXCLE操作实例
public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException {

InputStream ins = null;
Workbook wb = null;
ins = new FileInputStream(new File(file));
//ins= ExcelService.class.getClassLoader().getResourceAsStream(filePath);
wb = WorkbookFactory.create(ins);
ins.close();

//3.得到Excel工作表对象
Sheet sheet = wb.getSheetAt(0);
//总行数
int trLength = sheet.getLastRowNum();
//4.得到Excel工作表的行
Row row = sheet.getRow(0);
//总列数
int tdLength = row.getLastCellNum();
//5.得到Excel工作表指定行的单元格
Cell cell = row.getCell((short) 1);
//6.得到单元格样式
CellStyle cellStyle = cell.getCellStyle();

for (int i = 5; i < trLength; i++) {
//得到Excel工作表的行
Row row1 = sheet.getRow(i);
for (int j = 0; j < tdLength; j++) {
//得到Excel工作表指定行的单元格
Cell cell1 = row1.getCell(j);
/**
* 为了处理:Excel异常Cannot get a text value from a numeric cell
* 将所有列中的内容都设置成String类型格式
*/
if (cell1 != null) {
cell1.setCellType(CellType._NONE);
}
if (j == 5 && i <= 10) {
cell1.setCellValue("1000");
}

//获得每一列中的值
System.out.print(cell1 + " ");
}
System.out.println();
}

//将修改后的数据保存
OutputStream out = new FileOutputStream(file);
wb.write(out);

}

public static void CreateExcelDemo1() throws Exception {
List list = new ArrayList();
SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
// Student user1 = new Student(1, "张三", 16,true, df.parse("1997-03-12"));
// Student user2 = new Student(2, "李四", 17,true, df.parse("1996-08-12"));
// Student user3 = new Student(3, "王五", 26,false, df.parse("1985-11-12"));
// list.add(user1);
// list.add(user2);
// list.add(user3);

// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("学号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("年龄");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("性别");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("生日");
cell.setCellStyle(style);

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,

for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
// Student stu = (Student) list.get(i);
// // 第四步,创建单元格,并设置值
// row.createCell((short) 0).setCellValue((double) stu.getId());
// row.createCell((short) 1).setCellValue(stu.getName());
// row.createCell((short) 2).setCellValue((double) stu.getAge());
// row.createCell((short)3).setCellValue(stu.getSex()==true?"男":"女");
// cell = row.createCell((short) 4);
// cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
// .getBirthday()));
}
// 第六步,将文件存到指定位置
try {
FileOutputStream fout = new FileOutputStream("E:/students.xls");
wb.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String [] args) throws Exception{
removeFirstRow("C:\\data\\dqpt2\\CPQYB\\WeiXin-signcustomer20190604.csv","C:\\data\\dqpt2\\CPQYB\\signcustomer20190604.csv");
}
}

posted @ 2019-06-05 16:30  echoskk  阅读(324)  评论(0)    收藏  举报