Java 写rtf(word) excel文件

本文档提供了使用Java生成Excel和RTF(Word)文件的详细步骤。通过Apache POI库,展示了如何创建Excel工作表,设置单元格样式,合并单元格,以及写入内容。同时,还介绍了生成RTF文件的方法,包括设置字体、行距、添加图片等内容,以及如何使用iText库进行操作。
摘要由CSDN通过智能技术生成

 

二、下面就简单的解释一下这几个文件

 

1. ExcelGenerate .java文件 这个文件中每个方法都有解释,好好的看看注释

 

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 *
 * @author 岑
 *
 */
public class ExcelGenerate {

 public static HSSFWorkbook wb;
 
 private static HSSFSheet sheet;
 
 private static HSSFRow newRow;
 
 private static HSSFCellStyle cellSytle;
 
 private static HSSFCell newCell;
 
 ExcelGenerate() {
  wb = new HSSFWorkbook();
 }
 
 public HSSFWorkbook getInstance() {
  if (wb == null) {
   wb = new HSSFWorkbook();
  }
  return wb;
 }
 
 /**
  *  sheet 取得
  * @param sheet 对象 HSSFSheet
  */
 public static HSSFSheet getSheet(String sheetName) {
  sheet = wb.createSheet(sheetName);
  return sheet;
 }
 
 /**
  * 新建行
  *
  * @param sheet HSSFSheet 对象
  * @param rownum 行号 0:第一行 1:第2行...
  * @return row HSSFRow 对象
  */
 public static HSSFRow getNewRow(HSSFSheet sheet, int rownum) {
  newRow = sheet.createRow(rownum);
  return newRow;
 }
 
 public static HSSFCell getNewCell(HSSFRow row, int cellnum) {
  newCell = row.createCell(cellnum);
  return newCell;
 }
 /**
  * 合并单元格
  *
  * @param firstRow 开始行
  * @param lastRow  结束行
  * @param firstCol 开始列
  * @param lastCol  结束列
  */
 public static void CellMergedRegion(int firstRow, int lastRow, int firstCol, int lastCol) {
  CellRangeAddress region = new CellRangeAddress(firstRow, lastRow, firstCol,lastCol);
  sheet.addMergedRegion(region);
 }
 
 /**
  * 样式表
  *
  * @return CellStyle HSSFCellStyle
  */
 public static HSSFCellStyle getCellStyle() {
  // 创建单元格样式
  cellSytle = wb.createCellStyle();
  return cellSytle;
 }
 
 /**
  * 设置单元格边框
  *
  * @param colour 边框颜色 例如: HSSFColor.RED.index
  * @param borderBottom 边框底部 例如:HSSFCellStyle.BORDER_THIN
  * @param borderLeft 边框左边 例如:HSSFCellStyle.BORDER_THIN
  * @param borderRight 边框右边 例如:HSSFCellStyle.BORDER_THIN
  * @param borderTop 边框顶部 例如:HSSFCellStyle.BORDER_THIN
  * @see org.apache.poi.hssf.util.HSSFColor;
  * @see org.apache.poi.hssf.usermodel.HSSFCellStyle
  *
  */
 public static void setBorder(HSSFCellStyle sytle,short colour, short borderBottom, short borderLeft, short borderRight, short borderTop) {
  // 设置单元格边框
  sytle.setBottomBorderColor(colour);
  sytle.setBorderBottom(borderBottom);
  sytle.setBorderLeft(borderLeft);
  sytle.setBorderRight(borderRight);
  sytle.setBorderTop(borderTop);
 }
 
 /**
  * 设置单元格颜色
  *
  * @param colour 颜色设定 例如: HSSFColor.RED.index
  * @param fillPattern 填充颜色 例如:HHSSFCellStyle.SOLID_FOREGROUND
  * @see org.apache.poi.hssf.util.HSSFColor;
  * @see org.apache.poi.hssf.usermodel.HSSFCellStyle
  */
 public static void setForegroundColor(HSSFCellStyle sytle,int colour, int fillPattern) {
  // 设定单元格背景色
  sytle.setFillForegroundColor(HSSFColor.WHITE.index);
  sytle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
 }
 
 /**
  * 画表格
  *
  * @param startRow 开始行
  * @param endRow 结束行
  * @param startCell 开始格
  * @param endCell 结束格
  * @param cellSytle 表格样式
  */
 public static void setChart(int startRow, int endRow, int startCell, int endCell, HSSFCellStyle cellSytle){
  for (int i = startRow; i <= endRow; i++) {
   newRow = sheet.createRow(i);
   for (int j = startCell; j <= endCell; j++) {
    newCell = newRow.createCell(j);
    newCell.setCellStyle(cellSytle);
   }
  }
 }
 
 /**
  *
  * @param wb 工作表
  * @param filePath 文件名和路径
  * @throws IOException
  * @throws E

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值