SSH框架 POI例子小结

本文介绍如何利用Apache POI库创建Excel文件并设置多种样式。内容涵盖工作簿、工作表、行、单元格的基本操作及数据格式化、字体颜色、对齐方式等高级特性。

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

关于POI中创建excel表的各种小例子

通过建立excel表格最后输出到本地H:盘中

自己理解的:

1.首先建立工作簿——就是相当于建立一个文件

HSSFWorkbook wb = new HSSFWorkbook();

2.在工作簿上建立表空间,就是excel表格下面的sheet1,sheet2,sheet3

HSSFSheet sheet = wb.createSheet("first sheet");
		wb.createSheet("second sheet");

3.在表空间里面加第一行

HSSFRow row = sheet.createRow(0);


4.在第一行里面加入第一个cell单元格

HSSFCell cell = row.createCell(0);//第一行

5.对这个cell单元格进行各种style和value值的改变

cell.setCellValue(false);//第一行第一列写入value

6.最后通过outputStream流输出

wb.write(new FileOutputStream(new File("H:/poi.xls")));


难过不太会使csdn的东西







代码如下:

package com.emsp.website.safetyStudy.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
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;

public class PoiTest {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException {
		//工作薄
		HSSFWorkbook wb = new HSSFWorkbook();
		//工作表
		HSSFSheet sheet = wb.createSheet("first sheet");
		wb.createSheet("second sheet");
		//行
		HSSFRow row = sheet.createRow(0);
		//单元格
		HSSFCell cell = row.createCell(0);//第一行
		cell.setCellValue(false);//第一行第一列
		row.createCell(1).setCellValue(Calendar.getInstance());//第一行第二列:可以设置(boolean|Calendar|date|String|double|RichTextString)
		row.createCell(2).setCellValue(12345433.532343);//double型
		row.createCell(3).setCellValue("ddddddddddddddddddddddddddddddddddddddd");//String 型
		//创建数据格式对象
		HSSFDataFormat format = wb.createDataFormat();
		
		//格式化,设置单元格的样式
		HSSFCellStyle style = wb.createCellStyle();
		style.setDataFormat(format.getFormat("yyyy-HH-dd hh:mm:ss"));
		row.getCell(1).setCellStyle(style);//给第一行第二列添加正确的样式
		
		//double值格式化
		style = wb.createCellStyle();
		style.setDataFormat(format.getFormat("#,###.000"));//科学计数法运用
		row.getCell(2).setCellStyle(style);
		//设置列宽(单位:1/20点)
		sheet.setColumnWidth(1, 6000);
		sheet.autoSizeColumn(2);//自动扩宽第三列
		sheet.setColumnWidth(3, 7000);
		//自动回绕文本
		style = wb.createCellStyle();
		style.setWrapText(true);
		row.getCell(3).setCellStyle(style);
		
		
		
		
		/**
		 * 设置文本对齐方式
		 */
		row = sheet.createRow(1);
		row.createCell(0).setCellValue("左上");
		row.createCell(1).setCellValue("中中");
		row.createCell(2).setCellValue("右下");
		
		//设置行高
		row.setHeightInPoints(50);
		sheet.setColumnWidth(0, 5000);//设置列高
		sheet.setColumnWidth(1, 5000);
		sheet.setColumnWidth(2, 5000);
		//设置文本对齐方式--左上
		style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
		row.getCell(0).setCellStyle(style);//左上
		
		//设置文本对齐方式--中中
		style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置中
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//设置中
		row.getCell(1).setCellStyle(style);//左上
		
		//设置文本对齐方式--右下
		style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);
		row.getCell(2).setCellStyle(style);//左上
		
		
		//设置字体大小颜色
		style = row.getCell(1).getCellStyle();//取出中中的样式
		HSSFFont font = wb.createFont();
		font.setFontName("方正姚体");
		font.setFontHeightInPoints((short)30);
		font.setItalic(true);//斜体
		font.setColor(HSSFColor.RED.index);
		
		style.setFont(font);
		//字体旋转
		style = row.getCell(1).getCellStyle();
		style.setRotation((short)-30);
		
		/**
		 * 设置单元格格式---设置边框
		 * 
		 */
		row = sheet.createRow(2);
		cell = row.createCell(0);
		style = wb.createCellStyle();
		style.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);//双实线
		style.setTopBorderColor(HSSFColor.BLUE.index);
		
//		style.setFillForegroundColor(HSSFColor.GOLD.index);
		cell.setCellStyle(style);
		
		/**
		 * 单元格计算
		 */
		row = sheet.createRow(3);
		row.createCell(0).setCellValue(3);
		row.createCell(1).setCellValue(21);
		row.createCell(2).setCellValue(11);
		
		row.createCell(3).setCellFormula("sum(A4:C4)");
		//移动行
		sheet.shiftRows(1, 3, 2);//二三四向下串两行
		
		//拆分窗格
		//1000:左侧窗格的宽度;2000:上侧窗格的宽度;3:右侧窗格开始显示列的索引;4:下侧窗格开始显示行的索引;0:激活的面板区
//		sheet.createSplitPane(1000, 2000, 3, 4, 0);
		
		//冻结窗格
		//1:左侧冻结的列数;2:上侧冻结的行数;3:右侧窗格开始显示列的索引;4:下侧窗格开始显示行的索引;
		sheet.createFreezePane(1, 2, 3 ,4);
		
		//保存
		wb.write(new FileOutputStream(new File("H:/poi.xls")));
	}

}








                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值