1.添加maven依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
2.创建excel
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
public static void main(String args[]){
Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet=wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
Row row=sheet.createRow(2); // 创建一个行
row.setHeightInPoints(30);
createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);
createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);
createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);
FileOutputStream fileOut=new FileOutputStream("C:\\Users\\admin\\Desktop\\工作簿.xls");
wb.write(fileOut);
fileOut.close();
}
/**
* 创建一个单元格并为其设定指定的对齐方式
* @param wb 工作簿
* @param row 行
* @param column 列
* @param halign 水平方向对其方式
* @param valign 垂直方向对其方式
*/
private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
Cell cell=row.createCell(column); // 创建单元格
cell.setCellValue(new HSSFRichTextString("我在这")); // 设置值
CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式
cellStyle.setAlignment(halign); // 设置单元格水平方向对其方式
cellStyle.setVerticalAlignment(valign); // 设置单元格垂直方向对其方式
cell.setCellStyle(cellStyle); // 设置单元格样式
}
2.操作excel
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
//操作excel包含插入列,合并单元格,改单元格样式
public static void main(String args[]){
try {
File xlsFile = new File("C:\\Users\\admin\\Desktop\\工作簿.xls");
// 获得工作簿
Workbook workbook = WorkbookFactory.create(xlsFile);
// 获得工作表个数
int sheetCount = workbook.getNumberOfSheets();
// 遍历工作表
for (int i = 0; i < sheetCount; i++) {
Sheet readsheet = workbook.getSheetAt(i);
// 获得行数
int readrows = readsheet.getLastRowNum() + 1;
// 获得列数,先获得一行,在得到改行列数,此处默认得到的是0行。如果行数上的列数不一样就得循环处理
Row tmp = readsheet.getRow(0);
if (tmp == null) {
continue;
}
int readclos = tmp.getPhysicalNumberOfCells();
insercol(readsheet,readrows,readclos,workbook);
//保存文档
FileOutputStream fileOut=new FileOutputStream("C:\\Users\\admin\\Desktop\\工作簿1.xls");
workbook.write(fileOut);
fileOut.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 插入列数
* @param readsheet 遍历的那个sheet
* @param readrows 一共有多少行
* @param readclos 一共有多少列
* @param workbook
* @param n 插入多少列
* @param colnumber 在第几列之后插入
*/
public static void insercol(Sheet readsheet,int readrows,int readclos,Workbook workbook,int n,int colnumber){
// 读取数据
String [][] values=new String[readrows][readclos];
for (int readrow = 0; readrow < readrows; readrow++) {
Row r = readsheet.getRow(readrow);
for (int readcol = 0; readcol < readclos; readcol++) {
values[readrow][readcol]=r.getCell(readcol).getStringCellValue();
}
}
for (int readrow = 0; readrow < readrows; readrow++) {
Row r = readsheet.getRow(readrow);
Boolean colflag=false;
int number=0;
for (int readcol = 0; readcol < readclos+n; readcol++) {
if(colflag){
if(readcol>=readclos){
Cell createCell=r.createCell(readcol);
if(number<n){
createCell.setCellValue("我是新的列");
number++;
}else{
createCell.setCellValue(values[readrow][readcol-n]);
}
}else{
if(number<n){
r.getCell(readcol).setCellValue("我是新的列");
number++;
}else{
r.getCell(readcol).setCellValue(values[readrow][readcol-n]);
}
}
}
if(readcol==colnumber){
r.getCell(readcol).setCellValue("我是新的列");
//设置单元格的样式
CellStyle cellStyle=workbook.createCellStyle();
//背景色
cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
//前景色
cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
//边框
cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部边框
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部边框颜色
cellStyle.setBorderLeft(CellStyle.BORDER_THIN); // 左边边框
cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // 左边边框颜色
cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右边边框
cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); // 右边边框颜色
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边边框颜色
r.getCell(readcol).setCellStyle(cellStyle);
//合并单元格
readsheet.addMergedRegion(new CellRangeAddress(
readrow, // 起始行
readrow, // 结束行
readcol, // 其实列
readcol+1// 结束列
));
colflag=true;
number++;
}
}
}
}