當前位置:首頁 » 編程語言 » javaexcel生成

javaexcel生成

發布時間: 2023-01-14 17:53:54

A. 如何用java代碼將資料庫中的數據生成excel表

java 讀excel 還是比較方便簡單的,原理就是,先用java 讀取excel,然後,一行行的寫入資料庫,欄位的話,你自己程序裡面寫就行了,給你個例子:
從Excel讀取數據,生成新的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為名字,而為下標,從0開始
Sheet st = rwb.getSheet("original");
Cell c00 = st.getCell(0,0);
//通用的獲取cell值的方式,返回字元串
String strc00 = c00.getContents();
//獲得cell具體類型值的方式
if(c00.getType() == CellType.LABEL)
{
LabelCell labelc00 = (LabelCell)c00;
strc00 = labelc00.getString();
}
//輸出
System.out.println(strc00);
//關閉
rwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}

/**
* 輸出Excel
*
* @param os
*/
public static void writeExcel(OutputStream os)
{
try
{
/**
* 只能通過API提供的工廠方法來創建Workbook,而不能使用WritableWorkbook的構造函數,
* 因為類WritableWorkbook的構造函數為protected類型
* method(1)直接從目標文件中讀取WritableWorkbook wwb = Workbook.createWorkbook(new File(targetfile));
* method(2)如下實例所示 將WritableWorkbook直接寫入到輸出流

*/
WritableWorkbook wwb = Workbook.createWorkbook(os);
//創建Excel工作表 指定名稱和位置
WritableSheet ws = wwb.createSheet("Test Sheet 1",0);

//**************往工作表中添加數據*****************

//1.添加Label對象
Label label = new Label(0,0,"this is a label test");
ws.addCell(label);

//添加帶有字型Formatting對象
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對象
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對象
Number labelN = new Number(0,1,3.1415926);
ws.addCell(labelN);

//添加帶有formatting的Number對象
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對象
Boolean labelB = new jxl.write.Boolean(0,2,false);
ws.addCell(labelB);

//4.添加DateTime對象
jxl.write.DateTime labelDT = new jxl.write.DateTime(0,3,new java.util.Date());
ws.addCell(labelDT);

//添加帶有formatting的DateFormat對象
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);

//添加圖片對象,jxl只支持png格式圖片
File image = new File("f:\\2.png");
WritableImage wimage = new WritableImage(0,1,2,2,image);
ws.addImage(wimage);
//寫入工作表
wwb.write();
wwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}

/**
* 拷貝後,進行修改,其中file1為被對象,file2為修改後創建的對象
* 盡單元格原有的格式化修飾是不能去掉的,我們還是可以將新的單元格修飾加上去,
* 以使單元格的內容以不同的形式表現
* @param file1
* @param file2
*/
public static void modifyExcel(File file1,File file2)
{
try
{
Workbook rwb = Workbook.getWorkbook(file1);
WritableWorkbook wwb = Workbook.createWorkbook(file2,rwb);//
WritableSheet ws = wwb.getSheet(0);
WritableCell wc = ws.getWritableCell(0,0);
//判斷單元格的類型,做出相應的轉換
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();
}
}

//測試
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中做相關測試,創建一個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就可以動態生成Excel文檔了,其中response.setContentType("application/vnd.ms- excel");語句必須要,才能確保不亂碼,在jsp中輸入<%@page contentType="application/vnd.ms- excel;charset=GBK"%>不行。

B. 如何導出生成excel文件 java

編程中經常需要使用到表格(報表)的處理主要以Excel表格為主。下面給出用java寫入數據到excel表格方法:
1.添加jar文件

java導入導出Excel文件要引入jxl.jar包,最關鍵的是這套API是純Java的,並不依賴Windows系統,即使運行在Linux下,它同樣能夠正確的處理Excel文件。下載地址:http://www.andykhan.com/jexcelapi/

2.jxl對Excel表格的認識

可以參見http://www.cnblogs.com/xudong-bupt/archive/2013/03/19/2969997.html

3.java代碼根據程序中的數據生成上述圖片所示的t.xls文件

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

import java.io.File;
import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class Writer_excel{
public static void main(String[] args) {
//標題行
String title[]={"角色","編號","功能名稱","功能描述"};
//內容
String context[][]={{"UC11","設置課程","創建課程"},
{"UC12","設置學生名單","給出與課程關聯的學生名單"},
{"UC21","查看學生名單",""},
{"UC22","查看小組信息","顯示助教所負責的小組列表信息"}
};
//操作執行
try {
//t.xls為要新建的文件名
WritableWorkbook book= Workbook.createWorkbook(new File("t.xls"));
//生成名為「第一頁」的工作表,參數0表示這是第一頁
WritableSheet sheet=book.createSheet("第一頁",0);

//寫入內容
for(int i=0;i<4;i++) //title
sheet.addCell(new Label(i,0,title[i]));
for(int i=0;i<4;i++) //context
{
for(int j=0;j<3;j++)
{
sheet.addCell(new Label(j+1,i+1,context[i][j]));
}
}
sheet.addCell(new Label(0,1,"教師"));
sheet.addCell(new Label(0,3,"助教"));

/*合並單元格.合並既可以是橫向的,也可以是縱向的
*WritableSheet.mergeCells(int m,int n,int p,int q); 表示由(m,n)到(p,q)的單元格組成的矩形區域合並
* */
sheet.mergeCells(0,1,0,2);
sheet.mergeCells(0,3,0,4);

//寫入數據
book.write();
//關閉文件
book.close();
}
catch(Exception e) { }
}

C. java如何生成excel,具體的

poi或jxl都行
你從網上下載個jxl.jar的jar包
簡單說一下jxl導出excel過程
//先創建一個excel文件
String excelPath = "D:\\系統用戶.xls";
File excelFile = new File(excelPath);
if (!excelFile.exists()) {
excelFile.createNewFile();
}
WritableWorkbook book = Workbook.createWorkbook(excelFile);
WritableSheet ws = book.createSheet("用戶信息", 0); //新建一個sheet
---------------
//往表中添加內容
ws.addCell(WritableCell )
------------
//將數據寫入所建的excel
book.write();
book.close();

D. 利用java怎麼實現生成報表(Excel文件)

JAVA POI
組件
//創建HSSFWorkbook對象
HSSFWorkbook
wb
=
new
HSSFWorkbook();
//創建HSSFSheet對象
HSSFSheet
sheet
=
wb.createSheet("sheet0");
//創建HSSFRow對象
HSSFRow
row
=
sheet.createRow((short)0);
//創建HSSFCell對象
HSSFCell
cell=row.createCell((short)0);
//用來處理中文問題
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
//設置單元格的值
cell.setCellValue("單元格中的中文");
//定義你需要的輸出流
OutputStream
out
=
new
FileOutputStream("viwo.xls");
//輸出Excel

E. java怎麼導出excel表格

通過這個例子,演示以下如何用java生成excel文件:
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);//設置cell的整數類型的值
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);//設置cell浮點類型的值
row.createCell((short)2).setCellValue("test");//設置cell字元類型的值
row.createCell((short)3).setCellValue(true);//設置cell布爾類型的值
HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell樣式
cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//設置cell樣式為定製的日期格式
HSSFCell dCell =row.createCell((short)4);
dCell.setCellValue(new Date());//設置cell為日期類型的值
dCell.setCellStyle(cellStyle); //設置該cell日期的顯示格式
HSSFCell csCell =row.createCell((short)5);
csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//設置cell編碼解決中文高位位元組截斷
csCell.setCellValue("中文測試_Chinese Words Test");//設置中西文結合字元串
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();
}
}

F. java 怎樣解析 excel生成的xml文件

java解析excel生成的xml文件的方法是使用dom4j實現的。
dom4j是一個簡單的開源庫,用於處理XML、 XPath和XSLT,它基於Java平台,使用Java的集合框架,全面集成了DOM,SAX和JAXP。
1、excel生成的xml樣例文件:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Created>2006-09-16T00:00:00Z</Created>
<LastSaved>2016-07-25T03:26:50Z</LastSaved>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
<RemovePersonalInformation/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>7956</WindowHeight>
<WindowWidth>14808</WindowWidth>
<WindowTopX>240</WindowTopX>
<WindowTopY>168</WindowTopY>
<ActiveSheet>2</ActiveSheet>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="宋體" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s16" ss:Name="好">
<Font ss:FontName="宋體" x:CharSet="134" ss:Size="11" ss:Color="#006100"/>
<Interior ss:Color="#C6EFCE" ss:Pattern="Solid"/>
</Style>
<Style ss:ID="s17">
<Alignment ss:Horizontal="Left" ss:Vertical="Center" ss:Indent="1"
ss:WrapText="1"/>
<Font ss:FontName="宋體" x:CharSet="134" ss:Size="8" ss:Color="#686868"/>
<NumberFormat ss:Format="@"/>
</Style>
<Style ss:ID="s18" ss:Parent="s16">
<Alignment ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s19">
<NumberFormat ss:Format="yyyy/m/d\ h:mm:ss"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="6" ss:ExpandedRowCount="3" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="14.4">
<Row>
<Cell><Data ss:Type="String">工號</Data></Cell>
<Cell><Data ss:Type="String">姓名 </Data></Cell>
<Cell ss:Index="5"><Data ss:Type="String">工號</Data></Cell>
<Cell><Data ss:Type="String">姓名</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">111</Data></Cell>
<Cell><Data ss:Type="String">張三</Data></Cell>
<Cell ss:Index="5"><Data ss:Type="Number">111</Data></Cell>
<Cell ss:Formula="=VLOOKUP(R2C5:R3C5,RC[-5]:R[1]C[-4],2)"><Data
ss:Type="String">張三</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">112</Data></Cell>
<Cell><Data ss:Type="String">李四</Data></Cell>
<Cell ss:Index="5"><Data ss:Type="Number">112</Data></Cell>
<Cell ss:Formula="=VLOOKUP(R2C5:R3C5,RC[-5]:R[1]C[-4],2)"><Data
ss:Type="String">李四</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>7</ActiveRow>
<ActiveCol>5</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
2、java解析代碼:
import java.io.File;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("person.xml"));
Element root = document.getRootElement();

Iterator it = root.elementIterator();
while (it.hasNext()) {
Element element = (Element) it.next();

//未知屬性名稱情況下
/*Iterator attrIt = element.attributeIterator();
while (attrIt.hasNext()) {
Attribute a = (Attribute) attrIt.next();
System.out.println(a.getValue());
}*/

//已知屬性名稱情況下
System.out.println("id: " + element.attributeValue("id"));

//未知元素名情況下
/*Iterator eleIt = element.elementIterator();
while (eleIt.hasNext()) {
Element e = (Element) eleIt.next();
System.out.println(e.getName() + ": " + e.getText());
}
System.out.println();*/

//已知元素名情況下
System.out.println("title: " + element.elementText("title"));
System.out.println("author: " + element.elementText("author"));
System.out.println();
}
}
}

熱點內容
電腦配置低怎麼變得不卡 發布:2025-07-15 05:34:08 瀏覽:844
ios火影忍者手游腳本 發布:2025-07-15 05:31:34 瀏覽:82
iphone支付密碼忘了怎麼辦 發布:2025-07-15 05:30:55 瀏覽:775
c語言打開網頁 發布:2025-07-15 05:21:33 瀏覽:640
如何製作我的世界模組伺服器 發布:2025-07-15 05:21:33 瀏覽:903
phparray加 發布:2025-07-15 05:20:41 瀏覽:782
4000以內二手安卓機怎麼選 發布:2025-07-15 05:11:25 瀏覽:644
靜態編譯修復器 發布:2025-07-15 05:11:24 瀏覽:506
iphonexr的存儲空間 發布:2025-07-15 05:09:20 瀏覽:328
能緩存航海王 發布:2025-07-15 04:55:38 瀏覽:91