
maven依赖
<!--POI-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.9</version>
</dependency>
下面是源代码
import org.apache.poi.ss.usermodel.*
import org.apache.poi.util.IOUtils
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.io.*
public class POITest {
public static void main(String[] args) throws IOException {
Workbook workbook = new XSSFWorkbook(new FileInputStream("G:/demo3.xlsx"))
Sheet sheet1 = workbook.getSheet("Sheet1")
CellStyle rowStyle = sheet1.getRow(1).getRowStyle()
short height = sheet1.getRow(1).getHeight()
CellStyle cellStyle = sheet1.getRow(1).getCell(0).getCellStyle()
Row row2 = sheet1.createRow(2)
row2.setHeight(height)
row2.setRowStyle(rowStyle)
Cell cell0 = row2.createCell(0)
cell0.setCellValue("pawn")
cell0.setCellStyle(cellStyle)
Cell cell1 = row2.createCell(1)
cell1.setCellValue(19)
cell1.setCellStyle(cellStyle)
cellStyle=sheet1.getRow(1).getCell(2).getCellStyle()
Cell cell2 = row2.createCell(2)
cell2.setCellValue("1993年9月11日")
cell2.setCellStyle(cellStyle)
Cell cell3 = row2.createCell(3)
InputStream inputStream = new FileInputStream("C:\\Users\\admin\\Desktop\\20160114155540.png")
byte[] bytes = IOUtils.toByteArray(inputStream)
int i = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG)
Drawing drawing = sheet1.createDrawingPatriarch()
CreationHelper creationHelper = workbook.getCreationHelper()
ClientAnchor anchor = creationHelper.createClientAnchor()
// 定位图片位置
anchor.setRow1(2)
anchor.setCol1(3)
Picture picture = drawing.createPicture(anchor, i)
picture.resize()
OutputStream out = new FileOutputStream("G:/demo3.xlsx")
workbook.write(out)
out.close()
}
}