主pom:
顶端 <poi.version>3.12</poi.version>
<!-- poi start-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi.version}</version>
<scope>provided</scope>
</dependency>
<!-- poi end -->
子pom:
<!-- poi start -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
</dependency>
<!-- poi end -->
代码:
package com.loan.common.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class POITest {
public static void main(String[] args) {
//创建Workbook
HSSFWorkbook workbook = new HSSFWorkbook();
//样式
HSSFCellStyle setBorder = workbook.createCellStyle();
//字体
HSSFFont font = workbook.createFont();
//创建sheet
HSSFSheet sheet = workbook.createSheet("sheet1");
HSSFRow row = sheet.createRow(1);
HSSFCell cell = row.createCell(1);
cell.setCellValue("hello herman");
font.setFontName("仿宋_GB2312");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
font.setFontHeightInPoints((short) 22);
setBorder.setFillForegroundColor((short) 13);// 设置背景色
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
setBorder.setFont(font);//选择需要用到的字体格式
setBorder.setWrapText(true);//设置自动换行
cell.setCellStyle(setBorder);
try {
FileOutputStream outputStream = new FileOutputStream("/home/herman/123333/1111.xlsx");
workbook.write(outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//读取
read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void read() throws IOException {
try {
//读取excel文件
FileInputStream in = new FileInputStream("/home/herman/123333/1111.xlsx");
//将输入流转换为workbook对象
HSSFWorkbook workbook = new HSSFWorkbook(in);
//获取工作表
HSSFSheet sheet = workbook.getSheetAt(0);
//获取行
HSSFRow row = sheet.getRow(1);
//读取单元格
HSSFCell cell = row.getCell(1);
//得到单元格的内容
String value = cell.getStringCellValue();
//打印到控制台
System.out.println("B2的单元格内容为:"+value);
} catch (Exception e) {
// TODO: handle exception
}
}
}