實(shí)現(xiàn)代碼如下:

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請域名、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、清流網(wǎng)站維護(hù)、網(wǎng)站推廣。
import org.apache.poi.hssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
publicclass CreateCells
{
publicstaticvoid main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook對象
HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet對象
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);//建立新行
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);//建立新cell
cell.setCellValue(1);//設(shè)置cell的整數(shù)類型的值
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);//設(shè)置cell浮點(diǎn)類型的值
row.createCell((short)2).setCellValue("test");//設(shè)置cell字符類型的值
row.createCell((short)3).setCellValue(true);//設(shè)置cell布爾類型的值
HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell樣式
cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//設(shè)置cell樣式為定制的日期格式
HSSFCell dCell =row.createCell((short)4);
dCell.setCellValue(new Date());//設(shè)置cell為日期類型的值
dCell.setCellStyle(cellStyle); //設(shè)置該cell日期的顯示格式
HSSFCell csCell =row.createCell((short)5);
csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//設(shè)置cell編碼解決中文高位字節(jié)截斷
csCell.setCellValue("中文測試_Chinese Words Test");//設(shè)置中西文結(jié)合字符串
row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立錯誤cell
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}
Java是由Sun Microsystems公司推出的Java面向?qū)ο蟪绦蛟O(shè)計(jì)語言(以下簡稱Java語言)和Java平臺的總稱。由James Gosling和同事們共同研發(fā),并在1995年正式推出。Java最初被稱為Oak,是1991年為消費(fèi)類電子產(chǎn)品的嵌入式芯片而設(shè)計(jì)的。1995年更名為Java,并重新設(shè)計(jì)用于開發(fā)Internet應(yīng)用程序。
用Java實(shí)現(xiàn)的HotJava瀏覽器(支持Java applet)顯示了Java的魅力:跨平臺、動態(tài)Web、Internet計(jì)算。從此,Java被廣泛接受并推動了Web的迅速發(fā)展,常用的瀏覽器均支持Javaapplet。另一方面,Java技術(shù)也不斷更新。Java自面世后就非常流行,發(fā)展迅速,對C++語言形成有力沖擊。在全球云計(jì)算和移動互聯(lián)網(wǎng)的產(chǎn)業(yè)環(huán)境下,Java更具備了顯著優(yōu)勢和廣闊前景。2010年Oracle公司收購Sun Microsystems。
一、動態(tài)加載表格
1.首先在html中為表格的添加位置設(shè)置id
即是在html的body標(biāo)簽內(nèi)部寫一個div標(biāo)簽表明表格要添加到此div的內(nèi)部。如下
div id="tdl"div
2.在javascript中寫添加表格的語句
若在當(dāng)前html文件中,則寫在script標(biāo)簽內(nèi)部,如
代碼如下:
script type="text/javascript"
document.getElementById("tbl").innerHTML="tabletrtd/td/tr/table" //此處添加的表格可根據(jù)自己需要創(chuàng)建
/script
若是通過引入js文件,則在js文件(假設(shè)是test.js)中直接寫如下語句
代碼如下:
document.getElementById("tbl").innerHTML="tabletrtd/td/tr/table"
然后再引入自己的html文件
代碼如下:
script type="text/javascript" src="test.js"/script
二、 動態(tài)添加表格行
1.首先在html中為表格行的添加位置設(shè)置id,此位置必須是tbody內(nèi)部(不是特別準(zhǔn)確,但根據(jù)我的測試就得到此結(jié)論,有其他的方法請留言,謝謝),如下
代碼如下:
table
thead/thead
tfoottfoot //tfoot與thead是與tbody配套使用,但我在寫的時候,沒用也可以。
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中
項(xiàng)目結(jié)構(gòu):
xls:
\\\
XlsMain .java 類
//該類有main方法,主要負(fù)責(zé)運(yùn)行程序,同時該類中也包含了用poi讀取Excel(2003版)
*
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
* @author Hongten/br
*
* 參考地址:
*
*/
public class XlsMain {
public static void main(String[] args) throws IOException {
XlsMain xlsMain = new XlsMain();
XlsDto xls = null;
ListXlsDto list = xlsMain.readXls();
try {
XlsDto2Excel.xlsDto2Excel(list);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i list.size(); i++) {
xls = (XlsDto) list.get(i);
System.out.println(xls.getXh() + " " + xls.getXm() + " "
+ xls.getYxsmc() + " " + xls.getKcm() + " "
+ xls.getCj());
}
}
/**
* 讀取xls文件內(nèi)容
*
* @return ListXlsDto對象
* @throws IOException
* 輸入/輸出(i/o)異常
*/
private ListXlsDto readXls() throws IOException {
InputStream is = new FileInputStream("pldrxkxxmb.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
XlsDto xlsDto = null;
ListXlsDto list = new ArrayListXlsDto();
// 循環(huán)工作表Sheet
for (int numSheet = 0; numSheet hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循環(huán)行Row
for (int rowNum = 1; rowNum = hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
xlsDto = new XlsDto();
// 循環(huán)列Cell
// 0學(xué)號 1姓名 2學(xué)院 3課程名 4 成績
// for (int cellNum = 0; cellNum =4; cellNum++) {
HSSFCell xh = hssfRow.getCell(0);
if (xh == null) {
continue;
}
xlsDto.setXh(getValue(xh));
HSSFCell xm = hssfRow.getCell(1);
if (xm == null) {
continue;
}
xlsDto.setXm(getValue(xm));
HSSFCell yxsmc = hssfRow.getCell(2);
if (yxsmc == null) {
continue;
}
xlsDto.setYxsmc(getValue(yxsmc));
HSSFCell kcm = hssfRow.getCell(3);
if (kcm == null) {
continue;
}
xlsDto.setKcm(getValue(kcm));
HSSFCell cj = hssfRow.getCell(4);
if (cj == null) {
continue;
}
xlsDto.setCj(Float.parseFloat(getValue(cj)));
list.add(xlsDto);
}
}
return list;
}
/**
* 得到Excel表中的值
*
* @param hssfCell
* Excel中的每一個格子
* @return Excel中每一個格子中的值
*/
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布爾類型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回數(shù)值類型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串類型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}
}
XlsDto2Excel.java類
//該類主要負(fù)責(zé)向Excel(2003版)中插入數(shù)據(jù)
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class XlsDto2Excel {
/**
*
* @param xls
* XlsDto實(shí)體類的一個對象
* @throws Exception
* 在導(dǎo)入Excel的過程中拋出異常
*/
public static void xlsDto2Excel(ListXlsDto xls) throws Exception {
// 獲取總列數(shù)
int CountColumnNum = xls.size();
// 創(chuàng)建Excel文檔
HSSFWorkbook hwb = new HSSFWorkbook();
XlsDto xlsDto = null;
// sheet 對應(yīng)一個工作頁
HSSFSheet sheet = hwb.createSheet("pldrxkxxmb");
HSSFRow firstrow = sheet.createRow(0); // 下標(biāo)為0的行開始
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = "學(xué)號";
names[1] = "姓名";
names[2] = "學(xué)院";
names[3] = "課程名";
names[4] = "成績";
for (int j = 0; j CountColumnNum; j++) {
firstcell[j] = firstrow.createCell(j);
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
}
for (int i = 0; i xls.size(); i++) {
// 創(chuàng)建一行
HSSFRow row = sheet.createRow(i + 1);
// 得到要插入的每一條記錄
xlsDto = xls.get(i);
for (int colu = 0; colu = 4; colu++) {
// 在一行內(nèi)循環(huán)
HSSFCell xh = row.createCell(0);
xh.setCellValue(xlsDto.getXh());
HSSFCell xm = row.createCell(1);
xm.setCellValue(xlsDto.getXm());
HSSFCell yxsmc = row.createCell(2);
yxsmc.setCellValue(xlsDto.getYxsmc());
HSSFCell kcm = row.createCell(3);
kcm.setCellValue(xlsDto.getKcm());
HSSFCell cj = row.createCell(4);
cj.setCellValue(xlsDto.getCj());
(xlsDto.getMessage());
}
}
// 創(chuàng)建文件輸出流,準(zhǔn)備輸出電子表格
OutputStream out = new FileOutputStream("POI2Excel/pldrxkxxmb.xls");
hwb.write(out);
out.close();
System.out.println("數(shù)據(jù)庫導(dǎo)出成功");
}
}
XlsDto .java類
//該類是一個實(shí)體類
public class XlsDto {
/**
* 選課號
*/
private Integer xkh;
/**
* 學(xué)號
*/
private String xh;
/**
* 姓名
*/
private String xm;
/**
* 學(xué)院
*/
private String yxsmc;
/**
* 課程號
*/
private Integer kch;
/**
* 課程名
*/
private String kcm;
/**
* 成績
*/
private float cj;
public Integer getXkh() {
return xkh;
}
public void setXkh(Integer xkh) {
this.xkh = xkh;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public String getYxsmc() {
return yxsmc;
}
public void setYxsmc(String yxsmc) {
this.yxsmc = yxsmc;
}
public Integer getKch() {
return kch;
}
public void setKch(Integer kch) {
this.kch = kch;
}
public String getKcm() {
return kcm;
}
public void setKcm(String kcm) {
this.kcm = kcm;
}
public float getCj() {
return cj;
}
public void setCj(float cj) {
this.cj = cj;
}
}
后臺輸出:
數(shù)據(jù)庫導(dǎo)出成功
1.0 hongten 信息技術(shù)學(xué)院 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ) 80.0
2.0 王五 信息技術(shù)學(xué)院 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ) 81.0
3.0 李勝基 信息技術(shù)學(xué)院 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ) 82.0
4.0 五班古 信息技術(shù)學(xué)院 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ) 83.0
5.0 蔡詩蕓 信息技術(shù)學(xué)院 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ) 84.0
本文題目:java代碼實(shí)現(xiàn)表格 javaswing表格
鏈接分享:http://chinadenli.net/article4/doghpie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、商城網(wǎng)站、品牌網(wǎng)站制作、小程序開發(fā)、App開發(fā)、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)