java写入excel2007的操作

本文介绍如何使用org.apache.poi库操作Excel 2007文档,包括创建工作簿、设置字体样式及颜色,并将数据写入到指定路径的文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

org.apache.poi对更高excel2007文档的操作

1.导入jar包

dom4j-20040902.021138.jar

ooxml-schemas-1.1.jar

poi-3.9.jar

poi-ooxml-3.9.jar

xmlbeans-2.5.0.jar

2.案例代码

package xls;

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

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @description 操作excel2007
 * @url http://blog.youkuaiyun.com/howareyoutodaysoft
 * @qq 2481151614
 * @author YHZ
 * @date 2013-1-14
 *
 */
public class XlsxUtil {

	/**
	 * 写入
	 * @param path
	 */
	public void writeExcel2007(String path){
		XSSFWorkbook wb = new XSSFWorkbook();  //or new HSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("Fonts");
       
        Font font0 = wb.createFont();
        font0.setColor(IndexedColors.BROWN.getIndex());
        CellStyle style0 = wb.createCellStyle();
        style0.setFont(font0);
 
        Font font1 = wb.createFont();
        font1.setFontHeightInPoints((short)14);
        font1.setFontName("Courier New");
        font1.setColor(IndexedColors.RED.getIndex());
        CellStyle style1 = wb.createCellStyle();
        style1.setFont(font1);
 
        Font font2 = wb.createFont();
        font2.setFontHeightInPoints((short)16);
        font2.setFontName("Arial");
        font2.setColor(IndexedColors.GREEN.getIndex());
        CellStyle style2 = wb.createCellStyle();
        style2.setFont(font2);
 
        Font font3 = wb.createFont();
        font3.setFontHeightInPoints((short)18);
        font3.setFontName("Times New Roman");
        font3.setColor(IndexedColors.LAVENDER.getIndex());
        CellStyle style3 = wb.createCellStyle();
        style3.setFont(font3);
 
        Font font4 = wb.createFont();
        font4.setFontHeightInPoints((short)18);
        font4.setFontName("Wingdings");
        font4.setColor(IndexedColors.GOLD.getIndex());
        CellStyle style4 = wb.createCellStyle();
        style4.setFont(font4);
 
        Font font5 = wb.createFont();
        font5.setFontName("Symbol");
        CellStyle style5 = wb.createCellStyle();
        style5.setFont(font5);
 
        XSSFCell cell0 = sheet.createRow(0).createCell(1);
        cell0.setCellValue("Default");
        cell0.setCellStyle(style0);
 
        XSSFCell cell1 = sheet.createRow(1).createCell(1);
        cell1.setCellValue("Courier");
        cell1.setCellStyle(style1);
 
        XSSFCell cell2 = sheet.createRow(2).createCell(1);
        cell2.setCellValue("Arial中文内容");
        cell2.setCellStyle(style2);
 
        XSSFCell cell3 = sheet.createRow(3).createCell(1);
        cell3.setCellValue("Times New Roman");
        cell3.setCellStyle(style3);
 
        XSSFCell cell4 = sheet.createRow(4).createCell(1);
        cell4.setCellValue("Wingdings");
        cell4.setCellStyle(style4);
 
        XSSFCell cell5 = sheet.createRow(5).createCell(1);
        cell5.setCellValue("Symbol");
        cell5.setCellStyle(style5);
 
        // Write the output to a file
        FileOutputStream fileOut;
		try {
			fileOut = new FileOutputStream(path);
			wb.write(fileOut);
			fileOut.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		XlsxUtil test = new XlsxUtil();
		test.writeExcel2007("C:\\Documents and Settings\\不了了之\\桌面\\2007.xlsx");
	}
}

访问地址:http://www.massapi.com/class/xs/XSSFWorkbook.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值