资源简介
java利用POI技术实现Excel表格数据的导入并保存至数据库,导出数据至Excel表格
代码片段和文件信息
package org.apache.poi.ss.examples;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Map;
import java.util.HashMap;
/**
* A monthly calendar created using Apache POI. Each month is on a separate sheet.
*
* Usage:
* CalendarDemo -xls|xlsx
*
*
* @author Yegor Kozlov
*/
public class CalendarDemo {
private static final String[] days = {
“Sunday“ “Monday“ “Tuesday“
“Wednesday“ “Thursday“ “Friday“ “Saturday“};
private static final String[] months = {
“January“ “February“ “March““April“ “May“ “June““July“ “August“
“September““October“ “November“ “December“};
public static void main(String[] args) throws Exception {
Calendar calendar = Calendar.getInstance();
boolean xlsx = true;
for (String arg : args) {
if (arg.charAt(0) == ‘-‘) {
xlsx = arg.equals(“-xlsx“);
} else {
calendar.set(Calendar.YEAR Integer.parseInt(arg));
}
}
int year = calendar.get(Calendar.YEAR);
Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook();
// try (Workbook wb = xlsx ? new XSSFWorkbook() : new HSSFWorkbook()) {
try {
Mapyle> styles = createstyles(wb);
for (int month = 0; month < 12; month++) {
calendar.set(Calendar.MONTH month);
calendar.set(Calendar.DAY_OF_MONTH 1);
//create a sheet for each month
Sheet sheet = wb.createSheet(months[month]);
//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(80);
Cell titleCell = headerRow.createCell(0);
titleCell.setCellValue(months[month] + “ “ + year);
titleCell.setCellstyle(styles.get(“title“));
sheet.addMergedRegion(CellRangeAddress.valueOf(“$A$1:$N$1“));
//header with month titles
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
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-11-30 08:35 poi-master\
文件 754 2017-11-30 08:35 poi-master\.classpath
文件 69 2017-11-30 08:35 poi-master\.gitignore
文件 1036 2017-11-30 08:35 poi-master\.project
目录 0 2017-11-30 08:35 poi-master\.settings\
文件 503 2017-11-30 08:35 poi-master\.settings\.jsdtscope
文件 364 2017-11-30 08:35 poi-master\.settings\org.eclipse.jdt.core.prefs
文件 463 2017-11-30 08:35 poi-master\.settings\org.eclipse.wst.common.component
文件 345 2017-11-30 08:35 poi-master\.settings\org.eclipse.wst.common.project.facet.core.xm
文件 49 2017-11-30 08:35 poi-master\.settings\org.eclipse.wst.jsdt.ui.superType.container
文件 6 2017-11-30 08:35 poi-master\.settings\org.eclipse.wst.jsdt.ui.superType.name
文件 1091 2017-11-30 08:35 poi-master\LICENSE.md
文件 2058 2017-11-30 08:35 poi-master\README.md
目录 0 2017-11-30 08:35 poi-master\WebContent\
目录 0 2017-11-30 08:35 poi-master\WebContent\me
文件 39 2017-11-30 08:35 poi-master\WebContent\me
目录 0 2017-11-30 08:35 poi-master\WebContent\WEB-INF\
目录 0 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\
文件 73098 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\commons-codec-1.5.jar
文件 52915 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\commons-logging-1.1.jar
文件 313898 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\dom4j-1.6.1.jar
文件 245039 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\junit-4.11.jar
文件 358180 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\log4j-1.2.13.jar
文件 1949542 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\poi-3.10.1-20140818.jar
文件 312775 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\poi-examples-3.10.1-20140818.jar
文件 30534 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\poi-excelant-3.10.1-20140818.jar
文件 1035419 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\poi-ooxm
文件 4946391 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\poi-ooxm
文件 1239802 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\poi-scratchpad-3.10.1-20140818.jar
文件 26514 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\stax-api-1.0.1.jar
文件 2730866 2017-11-30 08:35 poi-master\WebContent\WEB-INF\lib\xm
............此处省略42个文件信息
- 上一篇:JAVA案例开发集锦87250
- 下一篇:SpringMVC上传图片文件到 腾讯云
评论
共有 条评论