慕丝7291255
poi版本。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
Hello World 示例
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellUtil;
/**
* @author Kevin Zou (kevinz@weghst.com)
*/
public class HelloWorld {
public static void main(String[] args) throws Exception {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("HELLO");
Map<String, Object> properties = new HashMap<>();
// border around a cell
properties.put(CellUtil.BORDER_TOP, CellStyle.BORDER_MEDIUM);
properties.put(CellUtil.BORDER_BOTTOM, CellStyle.BORDER_MEDIUM);
properties.put(CellUtil.BORDER_LEFT, CellStyle.BORDER_MEDIUM);
properties.put(CellUtil.BORDER_RIGHT, CellStyle.BORDER_MEDIUM);
// Give it a color (RED)
properties.put(CellUtil.TOP_BORDER_COLOR, IndexedColors.RED.getIndex());
properties.put(CellUtil.BOTTOM_BORDER_COLOR, IndexedColors.RED.getIndex());
properties.put(CellUtil.LEFT_BORDER_COLOR, IndexedColors.RED.getIndex());
properties.put(CellUtil.RIGHT_BORDER_COLOR, IndexedColors.RED.getIndex());
// Apply the borders to the cell at B2
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
for (Map.Entry<String, Object> e : properties.entrySet()) {
CellUtil.setCellStyleProperty(cell, workbook, e.getKey(), e.getValue());
}
cell.setCellValue("First"); // 单元格值
// Apply the borders to a 3x3 region starting at D4
for (int ix = 3; ix <= 5; ix++) {
row = sheet.createRow(ix);
for (int iy = 3; iy <= 5; iy++) {
cell = row.createCell(iy);
for (Map.Entry<String, Object> e : properties.entrySet()) {
CellUtil.setCellStyleProperty(cell, workbook, e.getKey(), e.getValue());
}
cell.setCellValue(ix + " * " + iy); // 单元格值
}
}
FileOutputStream fileOut = new FileOutputStream("C:/helloworld.xls");
workbook.write(fileOut);
fileOut.close();
System.out.println("The end.");
}
}
poi Quick Guide