Business Plan

The BusinessPlan application creates a sample business plan with three phases, weekly iterations and time highlighting. Demonstrates advanced cell formatting (number and date formats, alignments, fills, borders) and various settings for organizing data in a sheet (freezed panes, grouped rows).

/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */package org.apache.poi.ss.examples;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import java.util.Map;
import java.util.HashMap;
import java.util.Calendar;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat; /**
* A business plan demo
* Usage:
* BusinessPlan -xls|xlsx
*
* @author Yegor Kozlov
*/
public class BusinessPlan { private static SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM"); private static final String[] titles = {
"ID", "Project Name", "Owner", "Days", "Start", "End"}; //sample data to fill the sheet.
private static final String[][] data = {
{"1.0", "Marketing Research Tactical Plan", "J. Dow", "70", "9-Jul", null,
"x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x"},
null,
{"1.1", "Scope Definition Phase", "J. Dow", "10", "9-Jul", null,
"x", "x", null, null, null, null, null, null, null, null, null},
{"1.1.1", "Define research objectives", "J. Dow", "3", "9-Jul", null,
"x", null, null, null, null, null, null, null, null, null, null},
{"1.1.2", "Define research requirements", "S. Jones", "7", "10-Jul", null,
"x", "x", null, null, null, null, null, null, null, null, null},
{"1.1.3", "Determine in-house resource or hire vendor", "J. Dow", "2", "15-Jul", null,
"x", "x", null, null, null, null, null, null, null, null, null},
null,
{"1.2", "Vendor Selection Phase", "J. Dow", "19", "19-Jul", null,
null, "x", "x", "x", "x", null, null, null, null, null, null},
{"1.2.1", "Define vendor selection criteria", "J. Dow", "3", "19-Jul", null,
null, "x", null, null, null, null, null, null, null, null, null},
{"1.2.2", "Develop vendor selection questionnaire", "S. Jones, T. Wates", "2", "22-Jul", null,
null, "x", "x", null, null, null, null, null, null, null, null},
{"1.2.3", "Develop Statement of Work", "S. Jones", "4", "26-Jul", null,
null, null, "x", "x", null, null, null, null, null, null, null},
{"1.2.4", "Evaluate proposal", "J. Dow, S. Jones", "4", "2-Aug", null,
null, null, null, "x", "x", null, null, null, null, null, null},
{"1.2.5", "Select vendor", "J. Dow", "1", "6-Aug", null,
null, null, null, null, "x", null, null, null, null, null, null},
null,
{"1.3", "Research Phase", "G. Lee", "47", "9-Aug", null,
null, null, null, null, "x", "x", "x", "x", "x", "x", "x"},
{"1.3.1", "Develop market research information needs questionnaire", "G. Lee", "2", "9-Aug", null,
null, null, null, null, "x", null, null, null, null, null, null},
{"1.3.2", "Interview marketing group for market research needs", "G. Lee", "2", "11-Aug", null,
null, null, null, null, "x", "x", null, null, null, null, null},
{"1.3.3", "Document information needs", "G. Lee, S. Jones", "1", "13-Aug", null,
null, null, null, null, null, "x", null, null, null, null, null},
}; public static void main(String[] args) throws Exception {
Workbook wb; if(args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook();
else wb = new XSSFWorkbook(); Map<String, CellStyle> styles = createStyles(wb); Sheet sheet = wb.createSheet("Business Plan"); //turn off gridlines 关闭网格线
sheet.setDisplayGridlines(false);
sheet.setPrintGridlines(false);
sheet.setFitToPage(true);
sheet.setHorizontallyCenter(true);
PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true); //the following three statements are required only for HSSF
sheet.setAutobreaks(true);
printSetup.setFitHeight((short)1);
printSetup.setFitWidth((short)1); //the header row: centered text in 48pt font
Row headerRow = sheet.createRow(0);
headerRow.setHeightInPoints(12.75f);
for (int i = 0; i < titles.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(titles[i]);
cell.setCellStyle(styles.get("header"));
}
//columns for 11 weeks starting from 9-Jul
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR); calendar.setTime(fmt.parse("9-Jul"));
calendar.set(Calendar.YEAR, year);
for (int i = 0; i < 11; i++) {
Cell cell = headerRow.createCell(titles.length + i);
cell.setCellValue(calendar);
cell.setCellStyle(styles.get("header_date"));
calendar.roll(Calendar.WEEK_OF_YEAR, true);
}
//freeze the first row 冻结窗口
sheet.createFreezePane(0, 1); Row row;
Cell cell;
int rownum = 1;
for (int i = 0; i < data.length; i++, rownum++) {
row = sheet.createRow(rownum);
if(data[i] == null) continue; for (int j = 0; j < data[i].length; j++) {
cell = row.createCell(j);
String styleName;
boolean isHeader = i == 0 || data[i-1] == null;
switch(j){
case 0:
if(isHeader) {
styleName = "cell_b";
cell.setCellValue(Double.parseDouble(data[i][j]));
} else {
styleName = "cell_normal";
cell.setCellValue(data[i][j]);
}
break;
case 1:
if(isHeader) {
styleName = i == 0 ? "cell_h" : "cell_bb";
} else {
styleName = "cell_indented";
}
cell.setCellValue(data[i][j]);
break;
case 2:
styleName = isHeader ? "cell_b" : "cell_normal";
cell.setCellValue(data[i][j]);
break;
case 3:
styleName = isHeader ? "cell_b_centered" : "cell_normal_centered";
cell.setCellValue(Integer.parseInt(data[i][j]));
break;
case 4: {
calendar.setTime(fmt.parse(data[i][j]));
calendar.set(Calendar.YEAR, year);
cell.setCellValue(calendar);
styleName = isHeader ? "cell_b_date" : "cell_normal_date";
break;
}
case 5: {
int r = rownum + 1;
String fmla = "IF(AND(D"+r+",E"+r+"),E"+r+"+D"+r+",\"\")";
cell.setCellFormula(fmla);
styleName = isHeader ? "cell_bg" : "cell_g";
break;
}
default:
styleName = data[i][j] != null ? "cell_blue" : "cell_normal";
} cell.setCellStyle(styles.get(styleName));
}
} //group rows for each phase, row numbers are 0-based 分组
sheet.groupRow(4, 6);
sheet.groupRow(9, 13);
sheet.groupRow(16, 18); //set column widths, the width is measured in units of 1/256th of a character width 行间距
sheet.setColumnWidth(0, 256*6);
sheet.setColumnWidth(1, 256*33);
sheet.setColumnWidth(2, 256*20);
sheet.setZoom(75); //75% scale // Write the output to a file
String file = "businessplan.xls";
if(wb instanceof XSSFWorkbook) file += "x";
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close(); wb.close();
} /**
* create a library of cell styles
*/
private static Map<String, CellStyle> createStyles(Workbook wb){
Map<String, CellStyle> styles = new HashMap<>();
DataFormat df = wb.createDataFormat(); CellStyle style;
Font headerFont = wb.createFont();
     // 黑体
headerFont.setBold(true);
style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFont(headerFont);
styles.put("header", style); style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFont(headerFont);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("header_date", style); Font font1 = wb.createFont();
font1.setBold(true);
style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.LEFT);
style.setFont(font1);
styles.put("cell_b", style); style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.CENTER);
style.setFont(font1);
styles.put("cell_b_centered", style); style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.RIGHT);
style.setFont(font1);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_b_date", style); style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.RIGHT);
style.setFont(font1);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_g", style); Font font2 = wb.createFont();
     // 字体颜色
font2.setColor(IndexedColors.BLUE.getIndex());
font2.setBold(true);
style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.LEFT);
style.setFont(font2);
styles.put("cell_bb", style); style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.RIGHT);
style.setFont(font1);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_bg", style); Font font3 = wb.createFont();
// 字号
font3.setFontHeightInPoints((short)14);
font3.setColor(IndexedColors.DARK_BLUE.getIndex());
font3.setBold(true);
style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.LEFT);
style.setFont(font3);
style.setWrapText(true);
styles.put("cell_h", style); style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.LEFT);
style.setWrapText(true);
styles.put("cell_normal", style); style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.CENTER);
style.setWrapText(true);
styles.put("cell_normal_centered", style); style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.RIGHT);
style.setWrapText(true);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_normal_date", style); style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.LEFT);

      // 缩进

        style.setIndention((short)1);
style.setWrapText(true);
styles.put("cell_indented", style); style = createBorderedStyle(wb);
style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
styles.put("cell_blue", style); return styles;
} private static CellStyle createBorderedStyle(Workbook wb){
BorderStyle thin = BorderStyle.THIN;
short black = IndexedColors.BLACK.getIndex(); CellStyle style = wb.createCellStyle();
style.setBorderRight(thin);
style.setRightBorderColor(black);
style.setBorderBottom(thin);
style.setBottomBorderColor(black);
style.setBorderLeft(thin);
style.setLeftBorderColor(black);
style.setBorderTop(thin);
style.setTopBorderColor(black);
return style;
}
}

APache POI emaple ()的更多相关文章

  1. (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug

    如果POI-3.10往一个工作表(sheet)里面插入数据的话,需要注意了,其有一个不太被容易发现的bug. 被插入的工作表(sheet)里面的单元格没有包含任何的注解(comment)的时候,插入一 ...

  2. Apache POI组件操作Excel,制作报表(一)

    Apache的POI组件是Java操作Microsoft Office办公套件的强大API,其中对Word,Excel和PowperPoint都有支持,当然使用较多的还是Excel,因为Word和Po ...

  3. POI (Apache POI)

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 基本功能 编辑 结构: HSSF - 提供读写Mi ...

  4. Java开发小技巧(六):使用Apache POI读取Excel

    前言 在数据仓库中,ETL最基础的步骤就是从数据源抽取所需的数据,这里所说的数据源并非仅仅是指数据库,还包括excel.csv.xml等各种类型的数据接口文件,而这些文件中的数据不一定是结构化存储的, ...

  5. 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据

    1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...

  6. Apache POI组件操作Excel,制作报表(四)

    Apache POI组件操作Excel,制作报表(四) 博客分类: 探索实践 ExcelApacheSpringMVCServlet      上一篇我们介绍了如何制作复杂报表的分析和设计,本篇结合S ...

  7. Apache POI组件操作Excel,制作报表(三)

    Apache POI组件操作Excel,制作报表(三) 博客分类: 探索实践 ExcelApache算法Office单元测试      上一篇介绍了POI组件操作Excel时如何对单元格和行进行设置, ...

  8. Apache POI使用指南(HSSFWorkbook生成excel)

    说 明: 官网:http://poi.apache.org/ 由于poi的功能多样,可以生成ppt.word.excel.......,本文就以生成excel为例进行说明,相信聪明的你一定能举一反三 ...

  9. Apache POI 操作Excel(2)-- POI包引入项目

    Apache POI发行版包含对许多文档文件格式的支持.这种支持在几个Jar文件中提供.并非每种格式都需要所有jar.下表显示了POI组件.Maven存储库标记和项目的Jar文件之间的关系. (htt ...

随机推荐

  1. Codeforces Round #555 (Div. 3) 解题报告

    A.Reachable Numbers 题意: 给定操作f(x):将x加1,删去得到的数的所有末尾0,如f(10099)=10099+1=10100→1010→101.现在给定一个数n,对n进行无限次 ...

  2. 模拟、字符串--P1042 乒乓球 题解

    P1042 乒乓球 字符串string的基本使用 #include <iostream> #include <algorithm> #include <map> # ...

  3. mysql查询表中最小可用id值

    今天在看实验室的项目时,碰到的一个问题,.先把sql语句扔出来 // 这条语句在id没有1时,不能得到正确的查询结果. select min(id+1) from oslist c where not ...

  4. tomcat创建用户

    进入manager App时需要用户名+密码 输入错误时会出现页面如下: ​ 根据提示去服务器中找到tomcat目录下的tomcat-user.xml文件 在指定位置添加语句 <user use ...

  5. 7. ENGINES

    7. ENGINES ENGINES表提供有关存储引擎的信息. 这对于检查是否支持存储引擎或查看默认引擎是什么特别有用. INFORMATION_SCHEMA Name SHOW Name ENGIN ...

  6. 一篇文章掌握nightwatch自动化测试

    nightwatch.js是一个web-ui自动化测试框架,被vue-cli深度整合进来.如果一个项目是基于vue-cli搭建的,基本可以做到开箱即用. 但是我们不可能一直都使用vue-cli.因为它 ...

  7. Python的3种格式化字符串方法

    Python中有3种format字符串的方式: 传统C语言式 命名参数 位置参数 1. 传统C语言式 和c语言里面的 sprintf 类似,参数格式也一样 title = "world&qu ...

  8. 七牛云 X 英语流利说:教育 3.0 时代的智能突破

    美国当地时间 2018 年 9 月 27 日,国内领先的人工智能驱动的教育科技公司「英语流利说」正式挂牌纽交所,以其独创的教育 3.0 模式,成为中国「AI+ 教育」第一股. 教育 3.0 时代的智能 ...

  9. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  10. hdu 1500 dp

    /* 状态转移方程式: dp[i][j]=Min(dp[i][j-1],dp[i-1][j-2]+(a[j-1]-a[j])*(a[j-1]-a[j])); 依次求出第i个人在第j个数时的所求的最小值 ...