Java 如何使用ApachePOI打印(通过打印机)Excel工作表
我想用连接的打印机打印用ApachePOI创建的电子表格。怎么做?一年前我问过类似的问题,我不得不说:我不是说打开Java 如何使用ApachePOI打印(通过打印机)Excel工作表,java,excel,printing,apache-poi,Java,Excel,Printing,Apache Poi,我想用连接的打印机打印用ApachePOI创建的电子表格。怎么做?一年前我问过类似的问题,我不得不说:我不是说打开.xls文件并使用ctrl+p;) 表格如下: import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; import java.util.Locale; import java.util.Map;
.xls
文件并使用ctrl+p
;)
表格如下:
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
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.DataFormat;
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.xssf.usermodel.XSSFWorkbook;
public class BusinessPlan {
private static SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM", Locale.US);
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 }, };
/**
* create a library of cell styles
*/
private static Map<String, CellStyle> createStyles(Workbook wb) {
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
DataFormat df = wb.createDataFormat();
CellStyle style;
Font headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(headerFont);
styles.put("header", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(headerFont);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("header_date", style);
Font font1 = wb.createFont();
font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setFont(font1);
styles.put("cell_b", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(font1);
styles.put("cell_b_centered", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_b_date", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_g", style);
Font font2 = wb.createFont();
font2.setColor(IndexedColors.BLUE.getIndex());
font2.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setFont(font2);
styles.put("cell_bb", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.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.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setFont(font3);
style.setWrapText(true);
styles.put("cell_h", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setWrapText(true);
styles.put("cell_normal", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setWrapText(true);
styles.put("cell_normal_centered", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setWrapText(true);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_normal_date", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setIndention((short) 1);
style.setWrapText(true);
styles.put("cell_indented", style);
style = createBorderedStyle(wb);
style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
styles.put("cell_blue", style);
return styles;
}
private static CellStyle createBorderedStyle(Workbook wb) {
CellStyle style = wb.createCellStyle();
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
return style;
}
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);//style sa wkladane do mapy
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(3, 4);
// 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();
}
}
import java.io.FileOutputStream;
导入java.text.simpleDataFormat;
导入java.util.Calendar;
导入java.util.HashMap;
导入java.util.Locale;
导入java.util.Map;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellStyle;
导入org.apache.poi.ss.usermodel.DataFormat;
导入org.apache.poi.ss.usermodel.Font;
导入org.apache.poi.ss.usermodel.IndexedColors;
导入org.apache.poi.ss.usermodel.PrintSetup;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共类商业计划{
私有静态SimpleDataFormat fmt=新SimpleDataFormat(“dd MMM”,Locale.US);
私有静态最终字符串[]标题={“ID”、“项目名称”、“所有者”、“天数”、“开始”、“结束”};
//填写表格的样本数据。
私有静态最终字符串[][]数据={{“1.0”,“市场研究战术计划”,“J.道”,“70”,“9-Jul”,空,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”,“x”},空,{“1.1”,“范围定义阶段”,“J.道”,“10”,“9-Jul”,空,“x”,“x”,空,空,空,空,空,空,空,空,空,空,空,{“1.1.1”,“定义研究目标”、“J.Dow”、“3”、“9-Jul”、“null”、“x”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”}、{“1.1.2”、“定义研究要求”、“S.Jones”、“7”、“10-Jul”、“null”、“x”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“null”、“1.1.1.3”、“确定内部资源或雇佣供应商”、“J.Dow”、“2”、“7月15日”,null,“x”,“x”,null,null,null,null,null,null,null,null,null},null,{“1.2”,“供应商选择阶段”,“J.Dow”,“19”,“7月19日”,null,null,null,“x”,“x”,“x”,null,null,null,null,null},
{“1.2.1”、“定义供应商选择标准”、“J.Dow”、“3”、“7月19日”、“零、零”、“x”、“零、零、零、零、零、零、零”}、{“1.2.2”、“制定供应商选择问卷”、“S.Jones、T.Wates”、“2”、“7月22日”、“零、零、零”、“x”、“x”、“零、零、零、零、零、零、零、零、零”}、{“1.2.3”、“制定工作说明书”,“S.Jones”,“4”,“7月26日”,空,空,空,空,“x”,“x”,空,空,空,空,空,{“1.2.4”,“评估提案”,“J.Dow,S.Jones”,“4”,“2-Aug”,空,空,空,空,空,“x”,“x”,空,空,空,空,空,{“1.2.5”,“选择供应商”,“J.Dow”,“1”,“6-Aug”,空,空,空,空,空,空,空,空,null,null,null,null,null,null},null,{“1.3”,“研究阶段”,“G.Lee”,“47”,“9-Aug”,null,null,null,null,null,“x”,“x”,“x”,“x”,“x”},
{“1.3.1”、“开发市场研究信息需求调查问卷”、“G.Lee”、“2”、“9-Aug”、null、null、null、null、null、null、null、null、null}、{“1.3.2”、“市场研究需求访谈营销小组”、“G.Lee”、“2”、“8月11日”、null、null、null、null、null、null、“x”、“x”、null、null、null、null、null、null、null、null、null、{“1.3.3”,“文件信息需求”、“G.Lee,S.Jones”、“1”、“8月13日”,null,null,null,null,null,“x”,null,null,null,null,null,null},};
/**
*创建单元样式库
*/
专用静态映射createStyles(工作簿wb){
映射样式=新HashMap();
DataFormat df=wb.createDataFormat();
蜂窝式;
Font headerFont=wb.createFont();
表头.立根重量(字体.粗体重量\u粗体);
样式=createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColor.LIGHT\u矢车菊\u BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID\u前景);
style.setFont(headerFont);
样式。放置(“标题”,样式);
样式=createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColor.LIGHT\u矢车菊\u BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID\u前景);
style.setFont(headerFont);
setDataFormat(df.getFormat(“d-mmm”);
样式。放置(“标题\日期”,样式);
Font font1=wb.createFont();
font1.立根重量(Font.BOLDWEIGHT_BOLD);
样式=createBorderedStyle(wb);
style.setAlignment(单元格样式.左对齐);
style.setFont(font1);
样式。put(“cell_b”,样式);
样式=createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(font1);
样式。放置(“以单元格为中心”,样式);
样式=createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
setDataFormat(df.getFormat(“d-mmm”);
样式。放置(“单元格日期”,样式);
样式=createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID\u前景);
setDataFormat(df.getFormat(“d-mmm”);
样式。放置(“单元格”,样式);
Font font2=wb.createFont();
setColor(IndexedColors.BLUE.getIndex());
font2.setBoldweight(Font.BOLDWEIGHT\u BOLD);
样式=createBorderedStyle(wb);
style.setAlignment(单元格样式.左对齐);
style.setFont(font2);
样式。放置(“单元格_bb”,样式);
样式=createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID\u前景);