java操作excel(HSSF样式) .

java操作excel(HSSF样式一)
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
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;


HSSFCellStyle类代表一种单元格样式。可以通过这些类来设置单元格的边框样式、背景颜色、字体、水平和垂直方式的对齐。
如:
HSSFWorkbook workbook = new HSSFWorkbook(); //建立一个工作薄
HSSFCellStyle titleStyle=workbook.createCellStyle(); //在工作薄的基础上建立一个样式
titleStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE); //设置边框样式
titleStyle.setBorderLeft((short)1); //左边框
titleStyle.setBorderRight((short)1); //右边框
titleStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE); //顶边框
titleStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); //填充的背景颜色
titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //填充图案

假设什么定义了一个样式,想在填充第一个单元格的时候填充红,第二格单元格填充蓝色。
如果:
HSSFCellStyle cellStyle = workbook.createCellStyle(); //创建一个样式

cellStyle.setFillForegroundColor(HSSFColor.RED.index); //设置颜色为红色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

HSSFCell cell1 = row.createCell((short)1); //给单元格cell1填充红色
cell1.setCellStyle(cellStyle);

若: cellStyle.setFillForegroundColor(HSSFColor.BLUE.index); //设置颜色为蓝色

HSSFCell cell2 = row.createCell((short)2); //给单元格cell2填充蓝色
cell2.setCellStyle(cellStyle);

这个时候会出现的现象是单元格cell1和cell2都变成了蓝色。遇到这种情况,要预先定义两种不同的单元格样式。


当一个EXCEL文件同时需要很多大同小异的单元格样式时,这样一一定义很麻烦。POI HSSF提供了一个HSSFCellUtil类(在org.apache.poi. hssf.usermodel.contrib包),里面有几个方法可以绕过HSSFCellStyle直接设定单元格的样式,但这几个方法会抛出NestableException异 常,要处理这个异常,需要引用Apache的几个Common包:
commons-beanutils.jar
commons-beanutils-bean-collections.jar
commons-beanutils-core.jar
commons-lang.jar
commons-logging-api.jar

以下是其他各种情况的处理:
1、中文处理:
要在通过POI生成的EXCEL中正常显示中文,需要为单元格设置编码:
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("部门");

2、合并单元格:
HSSFSheet.addMergedRegion(new Region())方法可以合并单元格,Region()中的一个构造函数含有四个参数,分别代表起始行、起始列、结束 行、结束列:
sheet.addMergedRegion(new Region(initRow, (short)(initCol-2), initRow + lists.size() - 1, (short)(initCol-2)));

3、公式的处理:
HSSFCell.setCellFormula()方法用来在EXCEL单元格中写入公式。
cell = row.createCell((short)(dataFlag));
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula("SUM(" + getColLetter(initCol) + (listFlag+1) +":" + getColLetter(dataFlag-1) + (listFlag+1) + ")");
cell.setCellStyle(nameStyle);

4、链接的处理:
在POI中往单元格中写链接,是用HYPERLINK函数搞定的。
HYPERLINK函数包含两个参数,第一个参数是指向的URL地址,第二个参数是显示字串。
cell = row.createCell((short)(dataFlag));
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula("HYPERLINK(/"http://www.xxxxx.com/xxx.jsp?id=1/",/"homepage/")");
cell.setCellStyle(linkStyle);
为了使链接效果更好,我们可以给链接所在单元格定义一种样式,使链接显示为有下划线的蓝色字串:
HSSFCellStyle linkStyle = workbook.createCellStyle();
linkStyle.setBorderBottom((short)1);
linkStyle.setBorderLeft((short)1);
linkStyle.setBorderRight((short)1);
linkStyle.setBorderTop((short)1);
linkStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
linkStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = workbook.createFont();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setUnderline((byte)1);
font.setColor(HSSFColor.BLUE.index);
linkStyle.setFont(font);





另:

package *.*;

import java.util.ArrayList;
import java.util.Iterator;

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

public class FontCellStyle {
private static HSSFFont fontStyle = null;
private static HSSFCellStyle cellStyle = null;

/*设置字体格式*/
public static HSSFFont getHdrFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short)20);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
return fontStyle;
}
public static HSSFFont getFtrFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short)12);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
return fontStyle;
}
public static HSSFFont getContentFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short)12);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
return fontStyle;
}
public static HSSFFont getMergeConflictFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("Arial");
fontStyle.setFontHeightInPoints((short)12);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
return fontStyle;
}
/*设置Excel单元格格式,引用到字体格式*/
public static HSSFCellStyle getAnyCellStyle(HSSFWorkbook wb,HSSFFont font,short align,short valign,short indent,boolean wrapText) {
cellStyle =wb.createCellStyle();
if(font != null) cellStyle.setFont(font);
if(align > 0) cellStyle.setAlignment(align);
if(valign > 0) cellStyle.setVerticalAlignment(valign);
if(indent > 0) cellStyle.setIndention(indent);
cellStyle.setWrapText(wrapText);
return cellStyle;
}

/*设置Excel单元格行高、列宽*/
public static void setDefaultHighWidth(HSSFSheet sheet) {
sheet.setDefaultRowHeightInPoints(10);
sheet.setDefaultColumnWidth((short) 20);
}
public static void setDefaultCellHighWidthInRange(HSSFSheet sheet,short[] eachCellWidth,int high) {
//假定第一行和第一行所需的单元个已经建立好了,也就是说,在这之前已经调用了DesignXlsHeaderFooter.setXlsHeader
sheet.setDefaultRowHeightInPoints(high);//设置默认高
/*设置各列单元格宽度*/
for(int i = 0;i < eachCellWidth.length;i++) {
//System.out.print(""+i+"/t");
sheet.setColumnWidth((short) i,(short) ((eachCellWidth[i])*256));
}
//System.out.println();
/*
Iterator arrayItr = eachCellWidth.iterator();
short width;
short pos = 0;
while(arrayItr.hasNext()) {
width = Short.parseShort((String)arrayItr.next());
sheet.setColumnWidth(pos,width);
pos++;
}
*/
}//end_setDefaultCellHighWidthInRange
}


/*调用方式*/

/*设置整体excel单元格格式*/

FileOutputStream fos = null;
try {
fos = new FileOutputStream(rptRealPathAndName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
//System.out.println("创建文件失败。。。");
log.info("In WriteRptByType.writeRptTypeFive(),create file failed!!!");
log.error(e.getMessage());
//e.printStackTrace();
return -1;
}
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet();;
workBook.setSheetName(0,"移动",HSSFWorkbook.ENCODING_UTF_16);
HSSFCellStyle cellStyleHdr = FontCellStyle.getAnyCellStyle(workBook,FontCellStyle.getHdrFont(workBook),HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, (short)-1, true);

HSSFRow curRow = sheet.createRow(0);
HSSFCell curCell= curRow.createCell((short)0);
curCell.setEncoding(HSSFCell.ENCODING_UTF_16);
curCell.setCellStyle(cellStyleHdr );
curCell.setCellValue("可以写入汉字,无乱码");

/*.............写入文件.............*/

try {
workBook.write(fos);
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//System.out.println("写错误。。。");
succFlag = -1;
log.error("报表写错误:"+e.getMessage());
//e.printStackTrace();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值