package cn.ksource.test.execl;
|
|
import java.io.File;
|
import java.io.FileOutputStream;
|
|
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;
|
import org.apache.poi.hssf.util.HSSFColor;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
public class CreateExecl {
|
public static String outputFile = "D:/JTest/gongye.xls";
|
|
public final static int Column_Count = 12;
|
public final static int Row_Count = 24;
|
|
public static void main(String argv[]) {
|
|
try {
|
|
// 创建新的Excel 工作簿
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
// 在Excel工作簿中建一工作表,其名为缺省值
|
// 如要新建一名为"效益指标"的工作表,其语句为:
|
// HSSFSheet sheet = workbook.createSheet("效益指标");
|
HSSFSheet sheet = workbook.createSheet("效益指标");
|
|
CellStyle sheetStyle = workbook.createCellStyle();
|
sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
|
sheet.setDefaultColumnStyle(10, sheetStyle);
|
|
//默认列宽
|
sheet.setDefaultColumnWidth(9);
|
//默认行高
|
sheet.setDefaultRowHeight((short)300);
|
|
//合并单元格
|
sheet.addMergedRegion(new CellRangeAddress(0,0,0,Column_Count));
|
|
// 在索引0的位置创建行(最顶端的行)
|
HSSFRow row = sheet.createRow(0);
|
row.setHeight((short)600);
|
// 在索引0的位置创建单元格(左上端)
|
HSSFCell cell = row.createCell(0);
|
// 定义单元格为字符串类型
|
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
HSSFCellStyle cellStyle = workbook.createCellStyle();
|
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
|
|
/* cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
|
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
|
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
|
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
|
*/
|
cell.setCellStyle(cellStyle);
|
|
HSSFFont font2 = workbook.createFont();
|
font2.setFontName("楷体");
|
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
|
font2.setFontHeightInPoints((short) 20);
|
|
cellStyle.setFont(font2);
|
|
// 在单元格中输入一些内容
|
cell.setCellValue("郑州重点企业资料一览表");
|
|
for (int i = 0; i < Row_Count; i++) {
|
HSSFRow contentRow = sheet.createRow(i+1);
|
for (int j = 0; j <= Column_Count; j++) {
|
// 在索引0的位置创建单元格(左上端)
|
HSSFCell contentCell = contentRow.createCell(j);
|
// 定义单元格为字符串类型
|
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
HSSFCellStyle contentCellStyle = workbook.createCellStyle();
|
contentCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
|
|
contentCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
|
contentCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
|
contentCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
|
contentCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
|
|
HSSFFont contentFont = workbook.createFont();
|
contentFont.setFontName("仿宋");
|
contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//粗体显示
|
contentFont.setFontHeightInPoints((short) 12);
|
|
contentCellStyle.setFont(contentFont);
|
contentCell.setCellStyle(contentCellStyle);
|
}
|
}
|
|
|
|
// 新建一输出文件流
|
File file = new File(outputFile);
|
if (!file.exists()) {
|
file.getParentFile().mkdirs();
|
}
|
FileOutputStream fOut = new FileOutputStream(outputFile);
|
// 把相应的Excel 工作簿存盘
|
workbook.write(fOut);
|
fOut.flush();
|
// 操作结束,关闭文件
|
fOut.close();
|
System.out.println("文件生成...");
|
|
} catch (Exception e) {
|
System.out.println("已运行 xlCreate() : " + e);
|
}
|
}
|
|
}
|