jacob 生成Excel

直接上代码:

/**
  * 生成excel
  * 
  * @throws UnsupportedEncodingException
  */
private void moveToControlExcel(OutputStream os, List<SupervisionProblem> entityList)
			throws UnsupportedEncodingException {

		String HSSFname = "自动监控设施现场检查存在问题详表";
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet(HSSFname);
		sheet.getPrintSetup().setLandscape(true);

		// 创建标题样式
		HSSFCellStyle style = wb.createCellStyle();
		style.setWrapText(true); // 设置允许换行
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//  垂直   
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//  水平  
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
		// 设置字体
		HSSFFont font = wb.createFont();
		font.setFontName("华文楷体");
		font.setFontHeightInPoints((short) 16);// 设置字体大小
		style.setFont(font);//  样式,居中

		// 创建正文数据样式
		HSSFCellStyle style2 = wb.createCellStyle();
		style2.setWrapText(true); // 设置允许换行
		style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//  垂直   
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//  水平  
		style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
		style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
		style2.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
		style2.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
		HSSFFont font2 = wb.createFont();
		font2.setFontName("宋体");
		font2.setFontHeightInPoints((short) 8);
		style2.setFont(font2);// 选择需要用到的字体格式

		String fileName = new String(HSSFname.getBytes(), System.getProperty("file.encoding"));
		fileName = java.net.URLEncoder.encode(fileName, "UTF-8");

		// 创建第一行 标题
		// 设置第一行第一到第7个单元格合并
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
		HSSFRow rowTitle = sheet.createRow(0);
		for (int i = 0; i < 7; i++) {
			HSSFCell cellone = rowTitle.createCell(i); // 第一行第一列
			cellone.setCellValue(HSSFname);//  表格的第一行第一列显示的数据   
			cellone.setCellStyle(style);//  样式,居中
		}

		rowTitle.setHeight((short) 1100);

		// 第二行
		HSSFRow row2 = sheet.createRow(1);
		row2.setHeight((short) 480);
		String colums[] = new String[] { "序号", "企业名称", "问题发现时间", "问题开始时间", "整改期限", "问题类别", "现场检查发现的问题" };
		// 插入列名
		for (int i = 0; i < colums.length; i++) {
			HSSFCell row21 = row2.createCell(i);
			row21.setCellValue(new HSSFRichTextString(colums[i]));//  表格的第一行第一列显示的数据
			// 数据字体
			row21.setCellStyle(style2);
			if (i == 0) {
				sheet.setColumnWidth(i, 2000);// 设置单元格宽度
			} else if (i == 1) {
				sheet.setColumnWidth(i, 6500);// 设置单元格宽度
			} else if (i == 2) {
				sheet.setColumnWidth(i, 3000);// 设置单元格宽度
			} else if (i == 3) {
				sheet.setColumnWidth(i, 3000);// 设置单元格宽度
			} else if (i == 4) {
				sheet.setColumnWidth(i, 3000);// 设置单元格宽度
			} else if (i == 5) {
				sheet.setColumnWidth(i, 4500);// 设置单元格宽度
			} else if (i == 6) {
				sheet.setColumnWidth(i, 7300);// 设置单元格宽度
			}
		}

		// 插入数据
		for (int j = 0; j < entityList.size(); j++) {
			SupervisionProblem sp = entityList.get(j);
			HSSFRow rowData = sheet.createRow((2 + j));
			rowData.setHeight((short) 480);
			for (int i = 0; i < colums.length; i++) {
				HSSFCell cData = rowData.createCell(i);
				cData.setCellStyle(style2);
				if (i == 0) {
					cData.setCellValue(j + 1); // 序号
				} else if (i == 1) {
					cData.setCellValue(sp.getEnterpriseName());
				} else if (i == 2) {
					cData.setCellValue(sp.getCreateTime());
				} else if (i == 3) {
					cData.setCellValue(sp.getProblemStartTime());
				} else if (i == 4) {
					cData.setCellValue(sp.getRecEndTime());
				} else if (i == 5) {
					cData.setCellValue(sp.getProblemTypeName());
				} else if (i == 6) {
					cData.setCellValue(sp.getProblemDescription());
				}
			}
		}

		try {
			wb.write(os);
			os.close();
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
					os.flush();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (wb != null) {
				try {
					wb.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

 

一个简单示例 初始化当前Java线程STA ComThread InitSTA ; ActiveXComponent xl new ActiveXComponent "Excel Application" ; try{ 打开现有的Excel xl setProperty "Visible" new Variant false ; Dispatch workbooks xl getProperty "Workbooks" toDispatch ; Dispatch workbook Dispatch invoke workbooks "Open" Dispatch Method new Object[]{excelPath new Variant false 是否只读 new Variant false } 是否显示打开 new int[1] toDispatch ; 得到所有sheet Dispatch sheets Dispatch get workbook "sheets" toDispatch ; System out println "sheet数量:"+Dispatch get sheets "count" toString ; 根据索引得到sheet Dispatch sheet Dispatch invoke sheets "Item" Dispatch Get new Object[]{i} new int[1] toDispatch ; 绘图 Dispatch chartobjects Dispatch call sheet "ChartObjects" toDispatch ; Dispatch achart Dispatch invoke chartobjects "Add" Dispatch Method new Object[] { new Double 12 yHeight 横坐标 纵坐标 new Double 943 new Double 293 宽度 高度 } new int[1] toDispatch ; Dispatch chart Dispatch get achart "chart" toDispatch ; Dispatch range Dispatch invoke sheet "Range" Dispatch Get new Object[] {rangeX rangeY} 列名位置 数据位置 new int[2] toDispatch ; Dispatch series Dispatch call chart "seriescollection" toDispatch ; Dispatch call series "add" range new Variant true ; 生成图形 Dispatch put chart "PlotBy" "2" ; 纵向读取数据绘制chart 设置线条气泡颜色 黄色 Dispatch seriesChartFillFormat Dispatch get series "Fill" toDispatch ; Dispatch seriesForeColor Dispatch get seriesChartFillFormat "ForeColor" toDispatch ; Dispatch put s
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值