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++;
}
}
}
}