動(dòng)態(tài)創(chuàng)建表格,比如:str你從數(shù)據(jù)庫(kù)讀出列名,data數(shù)據(jù)集
成都創(chuàng)新互聯(lián)IDC提供業(yè)務(wù):西部信息服務(wù)器托管,成都服務(wù)器租用,西部信息服務(wù)器托管,重慶服務(wù)器租用等四川省內(nèi)主機(jī)托管與主機(jī)租用業(yè)務(wù);數(shù)據(jù)中心含:雙線機(jī)房,BGP機(jī)房,電信機(jī)房,移動(dòng)機(jī)房,聯(lián)通機(jī)房。
這里的data是一個(gè)二維數(shù)組,
就像
{{學(xué)號(hào):001,出生:09-01,成績(jī)99}
{學(xué)號(hào):001,出生:09-01,成績(jī)99}
{學(xué)號(hào):001,出生:09-01,成績(jī)99}}
生成表格
學(xué)號(hào) 出生日期 成績(jī)
001 09-01 99
001 09-01 98
001 09-01 99
java 讀excel 還是比較方便簡(jiǎn)單的,原理就是,先用java 讀取excel,然后,一行行的寫(xiě)入數(shù)據(jù)庫(kù),字段的話,你自己程序里面寫(xiě)就行了,給你個(gè)例子:
從Excel讀取數(shù)據(jù),生成新的Excel,以及修改Excel
package common.util;
import jxl.*;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.Boolean;
import java.io.*;
/**
* Created by IntelliJ IDEA.
* User: xl
* Date: 2005-7-17
* Time: 9:33:22
* To change this template use File | Settings | File Templates.
*/
public class ExcelHandle
{
public ExcelHandle()
{
}
/**
* 讀取Excel
*
* @param filePath
*/
public static void readExcel(String filePath)
{
try
{
InputStream is = new FileInputStream(filePath);
Workbook rwb = Workbook.getWorkbook(is);
//Sheet st = rwb.getSheet("0")這里有兩種方法獲取sheet表,1為名字,而為下標(biāo),從0開(kāi)始
Sheet st = rwb.getSheet("original");
Cell c00 = st.getCell(0,0);
//通用的獲取cell值的方式,返回字符串
String strc00 = c00.getContents();
//獲得cell具體類(lèi)型值的方式
if(c00.getType() == CellType.LABEL)
{
LabelCell labelc00 = (LabelCell)c00;
strc00 = labelc00.getString();
}
//輸出
System.out.println(strc00);
//關(guān)閉
rwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 輸出Excel
*
* @param os
*/
public static void writeExcel(OutputStream os)
{
try
{
/**
* 只能通過(guò)API提供的工廠方法來(lái)創(chuàng)建Workbook,而不能使用WritableWorkbook的構(gòu)造函數(shù),
* 因?yàn)轭?lèi)WritableWorkbook的構(gòu)造函數(shù)為protected類(lèi)型
* method(1)直接從目標(biāo)文件中讀取WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile));
* method(2)如下實(shí)例所示 將WritableWorkbook直接寫(xiě)入到輸出流
*/
WritableWorkbook wwb = Workbook.createWorkbook(os);
//創(chuàng)建Excel工作表 指定名稱(chēng)和位置
WritableSheet ws = wwb.createSheet("Test Sheet 1",0);
//**************往工作表中添加數(shù)據(jù)*****************
//1.添加Label對(duì)象
Label label = new Label(0,0,"this is a label test");
ws.addCell(label);
//添加帶有字型Formatting對(duì)象
WritableFont wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
WritableCellFormat wcf = new WritableCellFormat(wf);
Label labelcf = new Label(1,0,"this is a label test",wcf);
ws.addCell(labelcf);
//添加帶有字體顏色的Formatting對(duì)象
WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,
UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
Label labelCF = new Label(1,0,"This is a Label Cell",wcfFC);
ws.addCell(labelCF);
//2.添加Number對(duì)象
Number labelN = new Number(0,1,3.1415926);
ws.addCell(labelN);
//添加帶有formatting的Number對(duì)象
NumberFormat nf = new NumberFormat("#.##");
WritableCellFormat wcfN = new WritableCellFormat(nf);
Number labelNF = new jxl.write.Number(1,1,3.1415926,wcfN);
ws.addCell(labelNF);
//3.添加Boolean對(duì)象
Boolean labelB = new jxl.write.Boolean(0,2,false);
ws.addCell(labelB);
//4.添加DateTime對(duì)象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
ws.addCell(labelDT);
//添加帶有formatting的DateFormat對(duì)象
DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss");
WritableCellFormat wcfDF = new WritableCellFormat(df);
DateTime labelDTF = new DateTime(1,3,new java.util.Date(),wcfDF);
ws.addCell(labelDTF);
//添加圖片對(duì)象,jxl只支持png格式圖片
File image = new File("f:\\2.png");
WritableImage wimage = new WritableImage(0,1,2,2,image);
ws.addImage(wimage);
//寫(xiě)入工作表
wwb.write();
wwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 拷貝后,進(jìn)行修改,其中file1為被copy對(duì)象,file2為修改后創(chuàng)建的對(duì)象
* 盡單元格原有的格式化修飾是不能去掉的,我們還是可以將新的單元格修飾加上去,
* 以使單元格的內(nèi)容以不同的形式表現(xiàn)
* @param file1
* @param file2
*/
public static void modifyExcel(File file1,File file2)
{
try
{
Workbook rwb = Workbook.getWorkbook(file1);
WritableWorkbook wwb = Workbook.createWorkbook(file2,rwb);//copy
WritableSheet ws = wwb.getSheet(0);
WritableCell wc = ws.getWritableCell(0,0);
//判斷單元格的類(lèi)型,做出相應(yīng)的轉(zhuǎn)換
if(wc.getType == CellType.LABEL)
{
Label label = (Label)wc;
label.setString("The value has been modified");
}
wwb.write();
wwb.close();
rwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//測(cè)試
public static void main(String[] args)
{
try
{
//讀Excel
ExcelHandle.readExcel("f:/testRead.xls");
//輸出Excel
File fileWrite = new File("f:/testWrite.xls");
fileWrite.createNewFile();
OutputStream os = new FileOutputStream(fileWrite);
ExcelHandle.writeExcel(os);
//修改Excel
ExcelHandle.modifyExcel(new file(""),new File(""));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
2.在jsp中做相關(guān)測(cè)試,創(chuàng)建一個(gè)writeExcel.jsp
%
response.reset();//清除Buffer
response.setContentType("application/vnd.ms-excel");
File fileWrite = new File("f:/testWrite.xls");
fileWrite.createNewFile();
new FileOutputStream(fileWrite);
ExcelHandle.writeExcel(new FileOutputStream(fileWrite));
%
在IE中瀏覽writeExcel.jsp就可以動(dòng)態(tài)生成Excel文檔了,其中response.setContentType("application/vnd.ms- excel");語(yǔ)句必須要,才能確保不亂碼,在jsp中輸入%@page contentType="application/vnd.ms- excel;charset=GBK"%不行。
一、動(dòng)態(tài)加載表格
1.首先在html中為表格的添加位置設(shè)置id
即是在html的body標(biāo)簽內(nèi)部寫(xiě)一個(gè)div標(biāo)簽表明表格要添加到此div的內(nèi)部。如下
div id="tdl"div
2.在javascript中寫(xiě)添加表格的語(yǔ)句
若在當(dāng)前html文件中,則寫(xiě)在script標(biāo)簽內(nèi)部,如
代碼如下:
script type="text/javascript"
document.getElementById("tbl").innerHTML="tabletrtd/td/tr/table" //此處添加的表格可根據(jù)自己需要?jiǎng)?chuàng)建
/script
若是通過(guò)引入js文件,則在js文件(假設(shè)是test.js)中直接寫(xiě)如下語(yǔ)句
代碼如下:
document.getElementById("tbl").innerHTML="tabletrtd/td/tr/table"
然后再引入自己的html文件
代碼如下:
script type="text/javascript" src="test.js"/script
二、 動(dòng)態(tài)添加表格行
1.首先在html中為表格行的添加位置設(shè)置id,此位置必須是tbody內(nèi)部(不是特別準(zhǔn)確,但根據(jù)我的測(cè)試就得到此結(jié)論,有其他的方法請(qǐng)留言,謝謝),如下
代碼如下:
table
thead/thead
tfoottfoot //tfoot與thead是與tbody配套使用,但我在寫(xiě)的時(shí)候,沒(méi)用也可以。
tbody id="rows"/tbody
/table
[\s\S ]*\n
2.在javascript內(nèi)容中,要先創(chuàng)建行和單元格,再在.tbody中添加行,如下
[code]
row=document.createElement("tr"); //創(chuàng)建行
td1=document.createElement("tr"); //創(chuàng)建單元格
td1.appendChild(document.createTextNode("content")); //為單元格添加內(nèi)容
row.appendChild(td1); //將單元格添加到行內(nèi)
document.getElementById("rows").append(row); //將行添加到tbody中
import?java.sql.*;
public?class?Test
{
public?static?void?main(String[]?args)?throws?Exception
{
Class.forName("com.mysql.jdbc.Driver");
//一開(kāi)始必須填一個(gè)已經(jīng)存在的數(shù)據(jù)庫(kù)
String?url?=?"jdbc:mysql://localhost:3306/test?useUnicode=truecharacterEncoding=utf-8";????
Connection?conn?=?DriverManager.getConnection(url,?"root",?"123456");
Statement?stat?=?conn.createStatement();
//創(chuàng)建數(shù)據(jù)庫(kù)hello
stat.executeUpdate("create?database?hello");
//打開(kāi)創(chuàng)建的數(shù)據(jù)庫(kù)
stat.close();
conn.close();
url?=?"jdbc:mysql://localhost:3306/hello?useUnicode=truecharacterEncoding=utf-8";
conn?=?DriverManager.getConnection(url,?"root",?"123456");
stat?=?conn.createStatement();
//創(chuàng)建表test
stat.executeUpdate("create?table?test(id?int,?name?varchar(80))");
//添加數(shù)據(jù)
stat.executeUpdate("insert?into?test?values(1,?'張三')");
stat.executeUpdate("insert?into?test?values(2,?'李四')");
//查詢數(shù)據(jù)
ResultSet?result?=?stat.executeQuery("select?*?from?test");
while?(result.next())
{
System.out.println(result.getInt("id")?+?"?"?+?result.getString("name"));
}
//關(guān)閉數(shù)據(jù)庫(kù)
result.close();
stat.close();
conn.close();
}
}
import java.awt.*;
import javax.swing.*;
class Temp extends JFrame
{
JTextArea aa=new JTextArea(5,5);
Temp()
{
super("你要的表格");
Container cn=this.getContentPane();
cn.setLayout(new FlowLayout());
cn.add(aa);
this.setSize(300,300);
this.setVisible(true);
}
public static void main(String[] args)
{
new Temp();
}
}