POI 创建一个日历的excle

31 篇文章 0 订阅
18 篇文章 0 订阅
package wsc.cn.microsoft;


import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


/**
 * 
 * @author wsc
 *
 */
public class ClendarDemo {


private static final String[] days = { "星期天", "星期一", "星期二", "星期三", "星期四",
"星期五", "星期六" };


private static final String[] months = { "一月", "二月", "三月", "四月", "五月",
"六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };


public static void main(String[] args) throws Exception {


Calendar calendar = Calendar.getInstance();
boolean xlsx = true;
for (int i = 0; i < args.length; i++) {
if (args[i].charAt(0) == '-') {
xlsx = args[i].equals("-xlsx");
} else {
calendar.set(Calendar.YEAR, Integer.parseInt(args[i]));
}
}
int year = calendar.get(Calendar.YEAR);


Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook();


Map<String, CellStyle> styles = createStyles(wb);


for (int month = 0; month < 12; month++) {
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, 1);
// 创建每一个月份的表
Sheet sheet = wb.createSheet(months[month]);


// 设置表的边框
sheet.setDisplayGridlines(false);
sheet.setPrintGridlines(false);
sheet.setFitToPage(true);
sheet.setHorizontallyCenter(true);
PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true);


// 下面的三行设置只对台戏HSSF才有效
sheet.setAutobreaks(true);
printSetup.setFitHeight((short) 1);
printSetup.setFitWidth((short) 1);


// 设置首行的样式和内容
Row headerRow = sheet.createRow(0);
headerRow.setHeightInPoints(80);
Cell titleCell = headerRow.createCell(0);
titleCell.setCellValue(months[month] + " " + year);
titleCell.setCellStyle(styles.get("title"));
sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$N$1"));


// 设置首行的月份
Row monthRow = sheet.createRow(1);
for (int i = 0; i < days.length; i++) {
// set column widths, the width is measured in units of 1/256th
// of a character width
sheet.setColumnWidth(i * 2, 5 * 256); // the column is 5
// characters wide
sheet.setColumnWidth(i * 2 + 1, 13 * 256); // the column is 13
// characters wide
sheet.addMergedRegion(new CellRangeAddress(1, 1, i * 2,
i * 2 + 1));
Cell monthCell = monthRow.createCell(i * 2);
monthCell.setCellValue(days[i]);
monthCell.setCellStyle(styles.get("month"));
}
// 表格
int cnt = 1, day = 1;
int rownum = 2;
for (int j = 0; j < 6; j++) {
Row row = sheet.createRow(rownum++);
row.setHeightInPoints(100);
for (int i = 0; i < days.length; i++) {
Cell dayCell_1 = row.createCell(i * 2);
Cell dayCell_2 = row.createCell(i * 2 + 1);


int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
if (cnt >= day_of_week
&& calendar.get(Calendar.MONTH) == month) {
dayCell_1.setCellValue(day);
calendar.set(Calendar.DAY_OF_MONTH, ++day);


if (i == 0 || i == days.length - 1) {
dayCell_1.setCellStyle(styles.get("weekend_left"));
dayCell_2.setCellStyle(styles.get("weekend_right"));
} else {
dayCell_1.setCellStyle(styles.get("workday_left"));
dayCell_2.setCellStyle(styles.get("workday_right"));
}
} else {
dayCell_1.setCellStyle(styles.get("grey_left"));
dayCell_2.setCellStyle(styles.get("grey_right"));
}
cnt++;
}
if (calendar.get(Calendar.MONTH) > month)
break;
}
}


// 写到文件中去.
String file = "calendar.xls";
if (wb instanceof XSSFWorkbook)
file += "x";
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
}


/**
* 使用的单元格样式进行格式化的日历表
*/
private static Map<String, CellStyle> createStyles(Workbook wb) {
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();


short borderColor = IndexedColors.GREY_50_PERCENT.getIndex();


CellStyle style;
// 标题的样式
Font titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short) 48);
titleFont.setColor(IndexedColors.DARK_BLUE.getIndex());
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFont(titleFont);
styles.put("title", style);
// 月份的样式
Font monthFont = wb.createFont();
monthFont.setFontHeightInPoints((short) 12);
monthFont.setColor(IndexedColors.WHITE.getIndex());
monthFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(monthFont);
styles.put("month", style);

// 星期的样式
Font dayFont = wb.createFont();
dayFont.setFontHeightInPoints((short) 14);
dayFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE
.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(borderColor);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(borderColor);
style.setFont(dayFont);
styles.put("weekend_left", style);


style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE
.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(borderColor);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(borderColor);
styles.put("weekend_right", style);
//工作日的样式
style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setLeftBorderColor(borderColor);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(borderColor);
style.setFont(dayFont);
styles.put("workday_left", style);


style = wb.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(borderColor);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(borderColor);
styles.put("workday_right", style);


style = wb.createCellStyle();
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(borderColor);
styles.put("grey_left", style);


style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(borderColor);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(borderColor);
styles.put("grey_right", style);


return styles;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值