JExcelAPi下载地址:http://www.andykhan.com/jexcelapi/download.html,将解压后的文件目录下的jxl.jar导入到项目Proj_Excel中,在该项目下建立包bean.excel和resources,在bean.excel下建立类文件MutiStyleExcelWrite.java,在resources下导入图片文件du.png。
MutiStyleExcelWrite.java的源码:
package bean.excel;
import java.io.File;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Boolean;
import jxl.write.DateFormats;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class MutiStyleExcelWrite
{
public void createExcel(OutputStream os)
{
try
{
WritableWorkbook workbook=Workbook.createWorkbook(os); //创建工作区
WritableSheet sheet=workbook.createSheet("First Sheet", 0); //创建页
sheet.mergeCells(0, 0, 4, 0); /* mergeCells(int col1, int row1, int col2, int row2) */
WritableFont bold=new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);
WritableCellFormat titleFormate=new WritableCellFormat(bold);
titleFormate.setAlignment(jxl.format.Alignment.CENTRE);//单元格中的内容水平方向居中
titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//单元格中的内容垂直方向居中
Label title=new Label(0,0,"JExcelApi支持数据类型详细说明",titleFormate);
sheet.setRowView(0,600,false);//setRowView(int row, int height, boolean collapsed)
sheet.addCell(title);
WritableFont color=new WritableFont(WritableFont.ARIAL);//选择字体:无衬线体TrueType字型
color.setColour(Colour.GOLD);//设置字体颜色为金黄色
WritableCellFormat colorFormat=new WritableCellFormat(color);
Label format=new Label(0,1,"数据格式",colorFormat);
sheet.addCell(format);
Label floats=new Label(1,1,"浮点型");
sheet.addCell(floats);
Label integers=new Label(2,1,"整型");
sheet.addCell(integers);
Label booleans=new Label(3,1,"布尔型");
sheet.addCell(booleans);
Label dates=new Label(4,1,"日期格式");
sheet.addCell(dates);
Label example=new Label(0,2,"数据示列",colorFormat);
sheet.addCell(example);
WritableFont underline=new WritableFont(
WritableFont.ARIAL,
WritableFont.DEFAULT_POINT_SIZE,
WritableFont.NO_BOLD,
false,
UnderlineStyle.SINGLE); //设置下划线
WritableCellFormat greyBackground=new WritableCellFormat(underline);
greyBackground.setBackground(Colour.GRAY_25);
Number number=new Number(1,2,3.1415926535,greyBackground);
sheet.addCell(number);
WritableFont boldNumber=new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);
WritableCellFormat boldNumberFormate=new WritableCellFormat(boldNumber);
Number ints=new Number(2,2,15042699,boldNumberFormate);
sheet.addCell(ints);
Boolean bools=new Boolean(3,2,true);
sheet.addCell(bools);
WritableFont boldDate=new WritableFont(
WritableFont.ARIAL,
WritableFont.DEFAULT_POINT_SIZE,
WritableFont.BOLD,
false,
UnderlineStyle.SINGLE);
WritableCellFormat boldDateFormate=new WritableCellFormat(boldDate,DateFormats.FORMAT1);
Calendar c=Calendar.getInstance();
Date date=c.getTime();
DateTime dt=new DateTime(4,2,date,boldDateFormate);
sheet.addCell(dt);
/*图片: WritableImage(double x, double y, double width, double height, java.io.File image)*/
File imageFile=new File("J://myeclipse workspace/Proj_Excel/src/resources/du.png");
WritableImage wi=new WritableImage(0,3,5,4,imageFile);
sheet.addImage(wi);
workbook.write();
workbook.close();
os.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
MutiStyleExcelOutput.jsp的源码:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.io.*"%>
<%@page import="java.net.URLEncoder"%>
<%
String fname="JExcelApi支持数据类型详细说明";
OutputStream os=response.getOutputStream();
response.reset();
response.setCharacterEncoding("UTF-8");
fname=java.net.URLEncoder.encode(fname,"UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+new String(fname.getBytes("UTF-8"),"GBK")+".xls");
response.setContentType("application/msexcel");
bean.excel.MutiStyleExcelWrite sw=new bean.excel.MutiStyleExcelWrite();
sw.createExcel(os);
out.clear();
out = pageContext.pushBody();
%>
转载地址:http://wenku.baidu.com/view/2f6d761dc281e53a5802ff38.html?from=rec&pos=0