`
hellojim
  • 浏览: 52346 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

使用 poi 包生成(向.xls表格中写入)excel 表格文件

    博客分类:
  • JAVA
阅读更多
package com.jim.tools;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 此类采用 poi包 实现了向 .xls 文件写入的功能,
 * 目前为止 FastExcel 还没有提供向 .xls 文件写入的功能
 * @author  hellojim
 * @company cxtech
 */
public class POIWriteToExcel {
	
	private String URL;       //要写入的 .xls 文件的目录
	private String index;    //要写入的 .xls 文件的那个 Sheet 的名字, 默认为第0个(Sheet1)
	private int beginRow = 0; //要写入的 Sheet 的开始行, 默认为第0行
	private int beginCol = 0; //要写入的 Sheet 的开始列, 默认为第0列
	private String[][] content; //要写入到 Sheet 中的内容
	
	/**
	 *  URL 要写入的那个 .xls 文件的地址
	 *  content 要写入到 Sheet 中的内容
	 */
	public POIWriteToExcel(String URL,String[][] content) {
		//这里简单的做了个判断
		if(URL == null || URL.trim().equals("")) { 
			System.out.println("文件不能为空!");
		}else if(URL.trim().indexOf(".xls") == -1 && URL.trim().indexOf(".XLS") == -1) {
			System.out.println("文件格式不正确!");
		}else {
			this.URL = URL;
			this.content = content;
		}
	}
	
	/**
	 *  URL 要写入的那个 .xls 文件的地址
	 *  content 要写入到 Sheet 中的内容
	 *  index 要写入的那个 Sheet , 默认为第一个(Sheet1)
	 */
	public POIWriteToExcel(String URL,String[][] content, String index) {
		this(URL,content);
		this.index = index;
	}
	
	/**
	 *  URL 要写入的那个 .xls 文件的地址
	 *  content 要写入到 Sheet 中的内容
	 *  index 要写入的那个 Sheet , 默认为第一个(Sheet1)
	 *  beginRow 要写入的 Sheet 的开始行
	 *  beginCol 要写入的 Sheet 的开始列
	 */
	public POIWriteToExcel(String URL,String[][] content,String index,int beginRow,int beginCol) {
		this(URL,content,index);
		this.beginRow = beginRow;
		this.beginCol = beginCol;
	}
	
	public int getBeginCol() {
		return beginCol;
	}

	public void setBeginCol(int beginCol) {
		this.beginCol = beginCol;
	}

	public int getBeginRow() {
		return beginRow;
	}

	public void setBeginRow(int beginRow) {
		this.beginRow = beginRow;
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public String getURL() {
		return URL;
	}

	public void setURL(String url) {
		URL = url;
	}
	
	public String[][] getContent() {
		return content;
	}

	public void setContent(String[][] content) {
		this.content = content;
	}

	public void write() throws FileNotFoundException, IOException {
	
     //创建新的 Excel 工作簿
     HSSFWorkbook workbook = new HSSFWorkbook(); 
     //在 Excel 工作簿中建创一个工作表,其名为缺省值 sheet1
     HSSFSheet sheet = workbook.createSheet(String.valueOf(index));
     //创建字体
     HSSFFont font = workbook.createFont();
     //把字体颜色设置为红色
     font.setColor(HSSFFont.COLOR_NORMAL);
     //把字体设置为粗体
     font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
     //创建格式
     HSSFCellStyle cellStyle= workbook.createCellStyle();
     //把创建的字体付加于格式
     cellStyle.setFont(font);
     
     int beginRow = this.beginRow;
     int beginCol = this.beginCol;
     
     for(int i=0; i<content.length; i++) {
       //在工作表中创建一行
       HSSFRow row = sheet.createRow(beginRow++);
       beginCol = this.beginCol;
       for(int j=0; j<content[i].length; j++) {
         //在一行中创建一个表格
         HSSFCell cell = row.createCell((short)beginCol++);

         if( i == 0) {
           //把上面的格式付加于一个单元格
           cell.setCellStyle(cellStyle);
         }

         //设置此单元格中存入的是字符串
         cell.setCellType(HSSFCell.CELL_TYPE_STRING);
         //设置编码 这个是用来处理中文问题的
         cell.setEncoding(HSSFCell.ENCODING_UTF_16);
         //向此单元格中放入值
         cell.setCellValue(content[i][j]);
       }
     }
     
     FileOutputStream fileOutputStream = null;
     fileOutputStream = new FileOutputStream(this.URL);
     workbook.write(fileOutputStream);
     fileOutputStream.flush();
     fileOutputStream.close();
     
  }
  
  
  public static void main(String [] args) {
	  
	String[][] content = new String[2][3];
	
	content[0][0] = "1";
	content[0][1] = null;
	content[0][2] = "3";
	
	content[1][0] = "4";
	content[1][1] = "5";
	content[1][2] = "6";
	
    POIWriteToExcel poiwte = new POIWriteToExcel("d:\\abc.xls",content,"sheet5",5,5);
//    poiwte.setIndex("sheet3");
//    poiwte.setBeginRow(2);
//    poiwte.setBeginCol(3);
    
    try {
		poiwte.write();
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
}

好了,现在在 d:\ 目录下会有一个 abc.xls 文件

 

3
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics