JAva JXLによるEXCEL XLSファイルのダウンロード

9323 ワード

と書く
jxlをインポートする必要がある.JArパッケージ
public class GenExcel {
  public void createExcel(OutputStream os) throws WriteException,IOException{
	  //     
	  WritableWorkbook workbook = Workbook.createWorkbook(os);
	  //      
	  WritableSheet sheet = workbook.createSheet("First Sheet", 0);
	  //        ,       ,          ,       ,        
	  Label xuexiao = new Label(0,0,"  ");
	  sheet.addCell(xuexiao);
	   Label zhuanye = new Label(1,0,"  ");
       sheet.addCell(zhuanye);
       Label jingzhengli = new Label(2,0,"     ");
       sheet.addCell(jingzhengli);
       
       Label qinghua = new Label(0,1,"    ");
       sheet.addCell(qinghua);
       Label jisuanji = new Label(1,1,"     ");
       sheet.addCell(jisuanji);
       Label gao = new Label(2,1," ");
       sheet.addCell(gao);
       
       Label beida = new Label(0,2,"    ");
       sheet.addCell(beida);
       Label falv = new Label(1,2,"    ");
       sheet.addCell(falv);
       Label zhong = new Label(2,2," ");
       sheet.addCell(zhong);
       
       Label ligong = new Label(0,3,"      ");
       sheet.addCell(ligong);
       Label hangkong = new Label(1,3,"    ");
       sheet.addCell(hangkong);
       Label di = new Label(2,3," ");
       sheet.addCell(di);
       
       //             
       
       workbook.write();
       workbook.close();
       os.close();
  }
}
 
import java.io.IOException;
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.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class MutiStyleExcelWrite {
	public void createExcel(OutputStream os) throws WriteException,IOException{
		 //     
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        //      
        WritableSheet sheet = workbook.createSheet("First Sheet", 0);
        //    
        sheet.mergeCells(0, 0, 4, 0);//       ,         ,         ,         ,         
        WritableFont bold = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//           ,   Arial,     10,      
        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);//        
        sheet.addCell(title);
        
        //          
        WritableFont color = new WritableFont(WritableFont.ARIAL);//    
        color.setColour(Colour.GOLD);//          
        WritableCellFormat colorFormat = new WritableCellFormat(color);
        Label formate = new Label(0,1,"    ",colorFormat);
        sheet.addCell(formate);
        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);
        //             ,      
        workbook.write();
        workbook.close();
        os.close();
        
    }
}
 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<%@ page import="com.jxl.excel.*" %>
<% 
    String fname = "       ";
    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");//      
    GenExcel ge = new GenExcel();
    ge.createExcel(os);

 %>
<html>
  <head>
    <title></title>
  </head>
  
  <body>
  </body>
</html>
 
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="java.io.File"%>
<%@ page import="jxl.Cell"%>
<%@ page import="jxl.Sheet"%>
<%@ page import="jxl.Workbook"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'read.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<body>
		<font size="2"> <%
 	String fileName = "C:/Users/ZEN/Downloads/       .xls";
 	//             
 	File file = new File(fileName);
 	Workbook wb = Workbook.getWorkbook(file);//       Excel     
 	//        ,                  ,      。
 	Sheet sheet = wb.getSheet(0);
 	String outPut = "";

 	outPut = outPut + "<b>" + fileName + "</b><br>";
 	outPut = outPut + "   sheet    :" + sheet.getName() + "<br>";
 	outPut = outPut + "   sheet  :" + sheet.getRows() + " "
 			+ sheet.getColumns() + " <br>";
 	outPut = outPut + "      :<br>";
 	for (int i = 0; i < sheet.getRows(); i++) {
 		for (int j = 0; j < sheet.getColumns(); j++) {
 			Cell cell = sheet.getCell(j, i);
 			outPut = outPut + cell.getContents() + " ";
 		}
 		outPut = outPut + "<br>";
 	}
 	out.println(outPut);
 %> </font>
	</body>
</html>