Apache POI 应用另一篇
2008-06-26 13:19
183 查看
不说了,直接贴代码
package test;
import java.io.FileOutputStream;
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;
public class POI_ExcelDemo {
/**
* @param args
*/
public static void main(String[] args) {
/**
* author youjianbo
* date 2008-6-26
* 使用Apache POI生成Excel
* */
try
{
POI_ExcelDemo excelDemo = new POI_ExcelDemo();
String []values = {"a","b","c","d","e","f","j"};
//建立工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//建立Excel 页
HSSFSheet sheet = workbook.createSheet();
excelDemo.setColumnWidth(sheet);
//给sheet取名,并设置编码
workbook.setSheetName(0, "第一张sheet", HSSFWorkbook.ENCODING_UTF_16);
//建立Excel title
HSSFCellStyle style = excelDemo.createTitleStyle(workbook);
HSSFRow rowTitle = sheet.createRow(0);
for(int i=0;i<3;i++)
{
HSSFCell cellTitle = rowTitle.createCell((short)i);
cellTitle.setCellStyle(style);
cellTitle.setCellValue("The "+(i+1)+" Cell");
}
//建立Excel行,这里建立两行三列
for(int i=1;i<3;i++)
{
HSSFRow row = sheet.createRow(i);
for(int j=1;j<4;j++)
{
HSSFCell cell = row.createCell((short)(j-1));
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(style);
cell.setCellValue(values[i*j]);
}
}
//sheet.createFreezePane(3, 2);//分割工作区
//将生成的excel写入
String path = excelDemo.getClassPath();
System.out.println("获得的路径是:"+path);
FileOutputStream fileWrite = new FileOutputStream(path+"//"+"excelDemo.xls");
workbook.write(fileWrite);
fileWrite.flush();
fileWrite.close();
System.out.println("Excel 生成成功");
}catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
}
//获得工程目录路径
private String getClassPath()
{
return this.getClass().getClassLoader().getResource("").getPath();
}
//设置cell Style
private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
HSSFFont boldFont = wb.createFont();
boldFont.setFontHeight((short) 200);
HSSFCellStyle style = wb.createCellStyle();
style.setFont(boldFont);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
return style;
}
//设置Cell 宽度
private void setColumnWidth(HSSFSheet sheet)
{
//根据你数据里面的记录有多少列,就设置多少列
sheet.setColumnWidth((short) 0, (short) 3000);
sheet.setColumnWidth((short) 1, (short) 3000);
sheet.setColumnWidth((short) 2, (short) 3000);
}
}
package test;
import java.io.FileOutputStream;
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;
public class POI_ExcelDemo {
/**
* @param args
*/
public static void main(String[] args) {
/**
* author youjianbo
* date 2008-6-26
* 使用Apache POI生成Excel
* */
try
{
POI_ExcelDemo excelDemo = new POI_ExcelDemo();
String []values = {"a","b","c","d","e","f","j"};
//建立工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//建立Excel 页
HSSFSheet sheet = workbook.createSheet();
excelDemo.setColumnWidth(sheet);
//给sheet取名,并设置编码
workbook.setSheetName(0, "第一张sheet", HSSFWorkbook.ENCODING_UTF_16);
//建立Excel title
HSSFCellStyle style = excelDemo.createTitleStyle(workbook);
HSSFRow rowTitle = sheet.createRow(0);
for(int i=0;i<3;i++)
{
HSSFCell cellTitle = rowTitle.createCell((short)i);
cellTitle.setCellStyle(style);
cellTitle.setCellValue("The "+(i+1)+" Cell");
}
//建立Excel行,这里建立两行三列
for(int i=1;i<3;i++)
{
HSSFRow row = sheet.createRow(i);
for(int j=1;j<4;j++)
{
HSSFCell cell = row.createCell((short)(j-1));
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellStyle(style);
cell.setCellValue(values[i*j]);
}
}
//sheet.createFreezePane(3, 2);//分割工作区
//将生成的excel写入
String path = excelDemo.getClassPath();
System.out.println("获得的路径是:"+path);
FileOutputStream fileWrite = new FileOutputStream(path+"//"+"excelDemo.xls");
workbook.write(fileWrite);
fileWrite.flush();
fileWrite.close();
System.out.println("Excel 生成成功");
}catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
}
//获得工程目录路径
private String getClassPath()
{
return this.getClass().getClassLoader().getResource("").getPath();
}
//设置cell Style
private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
HSSFFont boldFont = wb.createFont();
boldFont.setFontHeight((short) 200);
HSSFCellStyle style = wb.createCellStyle();
style.setFont(boldFont);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
return style;
}
//设置Cell 宽度
private void setColumnWidth(HSSFSheet sheet)
{
//根据你数据里面的记录有多少列,就设置多少列
sheet.setColumnWidth((short) 0, (short) 3000);
sheet.setColumnWidth((short) 1, (short) 3000);
sheet.setColumnWidth((short) 2, (short) 3000);
}
}
相关文章推荐
- Apache POI 应用
- 微信的那个老外产品经理又写了一篇《中国移动应用设计趋势》
- Android应用升级构想和要点总结 //转一篇文章,慢慢看
- 转载一篇关于ALE的应用极好的文章
- Apache POI简单应用(HSSF)
- 转载一篇关于ALE的应用极好的文章
- 【转】ios voip应用退到后台的参考文章一篇 -------IOS开发之----详解在IOS后台执行
- (接上一篇)二叉树的基本应用举例
- Hibernate的Discriminator应用(一篇)
- EM算法在直线分类与灭点检测中的应用(关于一篇文章的读后感)
- 一篇比较好的KVO模式的解析与应用文章
- 新一篇: Message Queue(消息队列)介绍与应用
- 微信的那个老外产品经理,又写了一篇《中国移动应用设计趋势》
- 补一篇关于JMeter的cookies管理器应用
- 新一篇: 设计模式之管道和过滤器的应用
- 每日一篇:关于斐波拉契数列的应用
- 转的一篇 关于.net appDomain的东西 ,最后写的它的应用场合总结的不错...
- 转载一篇关于ALE的应用极好的文章
- 一篇很好的Ext+Json的应用文章
- 电子印章在企业中的应用,一篇看懂