您的位置:首页 > 编程语言 > Java开发

Java 生成一个excel表 9*9

2015-06-23 21:12 615 查看

工具

Apache 提供的POI,可以对Microsoft Excel, Word, PPT, Visio进行文件创建、读取等功能

实验目的

创建一个excel文件,内容是9*9表格



代码

java
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\workbook.xlsx");
String letter = "ABCDEFGHIJK";
String joint;
Workbook wb = new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("Sheet1");

CellStyle style = wb.createCellStyle(); //单元格样式
style.setAlignment(CellStyle.ALIGN_CENTER); //水平居中
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //垂直居中

for(int r = 0; r < 9; r++){
Row row = sheet1.createRow(r);
for(int c = 0; c < r + 1; c ++){
joint = letter.charAt(r) + "" + (c + 1);
Cell cell = row.createCell(c);
cell.setCellStyle(style);
//往cell写入公式
cell.setCellFormula("ROW(" + joint + ")&\"*\"&COLUMN(" + joint + ")&\"=\"&ROW(" + joint + ")*COLUMN(" + joint + ")");

}
}

//设置列宽
for(int i = 0; i < 9; i++)
sheet1.setColumnWidth(i, 10 * 256);

wb.write(fos);
fos.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: