🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #11836

�����jsp���excel�ļ�

#11836
From: JSP+ASP
Date: Fri, 27 Aug 2004 16:35
115 lines
6373 bytes
����:
Jakarta_POI ʹ��Java��дExcel(97-2002)�ļ�����������󲿷ֵ���Ҫ��
��Ϊ�պ���һ����Ŀʹ�õ���������ߣ����˵�ʱ��˳�㷭����һ��POI����
���һ��Guide.��һЩ�ڼ����޸ģ�ϣ���ʹ�������Ŀ����һЩ���Ű���
POI �����м�������Ŀ:HSSF����ʵ��Excel �Ķ�д.������HSSF����ҳ
http://jakarta.apache.org/poi/hssf/index.html
����Ľ����ǻ������µ�ַ�ķ��룺
http://jakarta.apache.org/poi/hssf/quick-guide.html
Ŀǰ�İ汾Ϊ1.51Ӧ���Ǻܳ�ʱ��֮�ڵ�һ���ȶ��棬��HSSF�ṩ��Sample���ǻ���
1.51��д������ʹ�õ�ʱ����Ҫ�ʵ���ע��.
��ʵPOI����ļ�������Ŀ���ز�ͬ��д Word ��HDF���ڿ�������.
XML�µ�FOP(http://xml.apache.org/fop/index.html)
�������pdf�ļ���Ҳ�DZȽϺõ�һ������
Ŀ¼:
����һ��workbook
����һ��sheet
����cells
��������cells
�趨��Ԫ���ʽ
˵����
���¿�����Ҫʹ�õ����µ���
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;
����workbook
HSSFWorkbook wb = new HSSFWorkbook();
//ʹ��Ĭ�ϵĹ��췽������workbook
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
//ָ���ļ���
wb.write(fileOut);
//������ļ�
fileOut.close();
����һ��sheet
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
//workbook����sheet
HSSFSheet sheet2 = wb.createSheet("second sheet");
//workbook���������sheet
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
����cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
//ע�����µĴ���ܶ෽���IJ�����short ����int ������Ҫ��һ������ת��
HSSFRow row = sheet.createRow((short)0);
//sheet ����һ��
HSSFCell cell = row.createCell((short)0);
//�д���һ����Ԫ��
cell.setCellValue(1);
//�趨��Ԫ���ֵ
//ֵ�����Ͳ����ж���double ,String ,boolean,
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
��������cells
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
//�趨ֵΪ����
cell.setCellValue(new Date());
HSSFCellStyle cellStyle = wb.createCellStyle();
//ָ��������ʾ��ʽ
cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));
cell = row.createCell((short)1);
cell.setCellValue(new Date());
//�趨��Ԫ��������ʾ��ʽ
cell.setCellStyle(cellStyle);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
�趨��Ԫ���ʽ
��Ԫ���ʽ���趨�кܶ���ʽ������Ԫ��Ķ��뷽ʽ�����ݵ��������ã�
��Ԫ��ı���ɫ�ȣ���Ϊ��ʽ�Ƚ϶ֻ࣬��һЩ����.���µ�������
POI1.5�п��ܻ�����ı����鿴API.
.........
// Aqua background
HSSFCellStyle style = wb.createCellStyle();
//����һ����ʽ
style.setFillBackgroundColor(HSSFCellStyle.AQUA);
//�趨����ʽ�ĵı�����ɫ���
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
//��ʽ��������͡�
//�ж���ʽ����:
//HSSFCellStyle.BIG_SPOTS
//HSSFCellStyle.FINE_DOTS
//HSSFCellStyle.SPARSE_DOTS��
style.setAlignment(HSSFCellStyle.ALIGN_CENTER );
//�����
style.setFillBackgroundColor(HSSFColor.GREEN.index);
//�趨��Ԫ��������ɫ
style.setFillForegroundColor(HSSFColor.RED.index);
//���õ�Ԫ����ʾ��ɫ
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);

--
90%������Ҫ���Լ����������
              ����������ˣ���ô���������Խ���������ˮƽԽ�ߣ���
              �����û����������ô���������Խ��������Խ��Խ�ӽ����
          ��֪����˵�Ķ����ϻ�!!
     �����ش������������ű��˵�����ʾ�����ɵ���ʧ��
      ���˸������κ�����.
�� ��Դ:�����Ļ����� bbs.cqupt.edu.cn��[FROM: LoveLonely.com] 
Technical Details
Message-ID: <4E0dJM$AKV@bbs.cqupt.edu.cn>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news3.optonline.net!news.bylinux.net!news2.happynet.org!news.uestc.edu.cn!CQUPT

Show Full Headers
Complete Article Headers
X-Google-Thread: f6cb0,ecd6497c8e8d9221,start
X-Google-Attributes: gidf6cb0,public
X-Google-Language: Chinese,GB
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!news3.optonline.net!news.bylinux.net!news2.happynet.org!news.uestc.edu.cn!CQUPT
From: jaket.bbs@bbs.cqupt.edu.cn (JSP+ASP)
Newsgroups: cn.bbs.comp.lang.java
Subject: �����jsp���excel�ļ�
Date: 27 Aug 2004 16:35:08 GMT
Organization: ��������
Message-ID: <4E0dJM$AKV@bbs.cqupt.edu.cn>
X-Filename: Java/M.1093624486.A
Lines: 115