cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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);
        }
    }
 
}