java achieve excel import and export (2) (transfer)

================= ===================== Summary
java how to do Excel (data import and export) (change)

jxl.jar, can go to http://sourceforge.net/project/showfiles.php?group_id=79926 download.

1. To read the contents of Excel files

java code
/ ** Read the contents of Excel files
* @ Param file the file to be read
* @ Return
* /
public static String readExcel (File file) (
StringBuffer sb = new StringBuffer ();

Workbook wb = null;
try (
/ / Construction Workbook (workbook) object
wb = Workbook.getWorkbook (file);
) Catch (BiffException e) (
e.printStackTrace ();
) Catch (IOException e) (
e.printStackTrace ();
)

if (wb == null)
return null;

/ / Get the Workbook object, you can get through it Sheet (sheet) object of
Sheet [] sheet = wb.getSheets ();

if (sheet! = null & & sheet.length> 0) (
/ / Loop for each work table
for (int i = 0; i
/ / Get the current number of rows in the worksheet
int rowNum = sheet [i]. getRows ();
for (int j = 0; j
/ / Get the current line of all cells
Cell [] cells = sheet [i]. GetRow (j);
if (cells! = null & & cells.length> 0) (
/ / Loop for each cell
for (int k = 0; k
/ / Read the value of the current cell
String cellValue = cells [k]. GetContents ();
sb.append (cellValue + "\ t");
)
)
sb.append ("\ r \ n");
)
sb.append ("\ r \ n");
)
)
/ / Finally close the resource, release the memory
wb.close ();
return sb.toString ();
)
2. Write to Excel file

There are many formats, and example text bold, add some color, you can refer jxl of the api.
It also recommends a good article: http://www.ibm.com/developerworks/cn/java/l-javaExcel/?ca=j-t10

java code
/ ** Generate an Excel file
* @ Param fileName the file name to generate the Excel
* /
public static void writeExcel (String fileName) (
WritableWorkbook wwb = null;
try (
/ / First of all, to use the Workbook class factory method to create a writable workbook (Workbook) object
wwb = Workbook.createWorkbook (new File (fileName));
) Catch (IOException e) (
e.printStackTrace ();
)
if (wwb! = null) (
/ / Create a writable sheet
/ / Workbook for createSheet method has two parameters, the first is the name of the worksheet, the second is a worksheet in the workbook's location
WritableSheet ws = wwb.createSheet ("sheet1", 0);

/ / Start adding the following cell
for (int i = 0; i <10; i + +) (
for (int j = 0; j <5; j + +) (
/ / It should be noted that, in Excel, the first parameter, said column, said second row
Label labelC = new Label (j, i, "This is the first" + (i +1) + "at line" + (j +1) + "column");
try (
/ / Will be added to the cells generated worksheet
ws.addCell (labelC);
) Catch (RowsExceededException e) (
e.printStackTrace ();
) Catch (WriteException e) (
e.printStackTrace ();
)

)
)

try (
/ / Write the file from memory
wwb.write ();
/ / Close the resources, the release of memory
wwb.close ();
) Catch (IOException e) (
e.printStackTrace ();
) Catch (WriteException e) (
e.printStackTrace ();
)
)
)
3. In an Excel file, it contains a certain keyword search

java code
/ ** Search for a particular file has a keyword
* @ Param file file to be searched
* @ Param keyWord keywords to search
* @ Return
* /
public static boolean searchKeyWord (File file, String keyWord) (
boolean res = false;

Workbook wb = null;
try (
/ / Construction Workbook (workbook) object
wb = Workbook.getWorkbook (file);
) Catch (BiffException e) (
return res;
) Catch (IOException e) (
return res;
)

if (wb == null)
return res;

/ / Get the Workbook object, you can get through it Sheet (sheet) object of
Sheet [] sheet = wb.getSheets ();

boolean breakSheet = false;

if (sheet! = null & & sheet.length> 0) (
/ / Loop for each work table
for (int i = 0; i
if (breakSheet)
break;

/ / Get the current number of rows in the worksheet
int rowNum = sheet [i]. getRows ();

boolean breakRow = false;

for (int j = 0; j
if (breakRow)
break;
/ / Get the current line of all cells
Cell [] cells = sheet [i]. GetRow (j);
if (cells! = null & & cells.length> 0) (
boolean breakCell = false;
/ / Loop for each cell
for (int k = 0; k
if (breakCell)
break;
/ / Read the value of the current cell
String cellValue = cells [k]. GetContents ();
if (cellValue == null)
continue;
if (cellValue.contains (keyWord)) (
res = true;
breakCell = true;
breakRow = true;
breakSheet = true;
)
)
)
)
)
)
/ / Finally close the resource, release the memory
wb.close ();

return res;
)
4. To insert a picture into Excel icon

Implementation is easy to insert a picture, see the following code:

java code
/ ** Insert a picture into Excel
* @ Param dataSheet worksheet to be inserted
* @ Param col the column picture from the beginning
* @ Param row image from the beginning of the line
* @ Param width the number of columns share pictures
* @ Param height number of rows share pictures
* @ Param imgFile to insert the image file
* /
public static void insertImg (WritableSheet dataSheet, int col, int row, int width,
int height, File imgFile) (
WritableImage img = new WritableImage (col, row, width, height, imgFile);
dataSheet.addImage (img);
)
Note the above code is very clear, probably also do not have to explain, we can use the following procedures for verification:

java code
try (
/ / Create a workbook
WritableWorkbook workbook = Workbook.createWorkbook (new File ("D: / test1.xls"));
/ / To be inserted worksheets
WritableSheet imgSheet = workbook.createSheet ("Images", 0);
/ / To insert the picture file
File imgFile = new File ("D: / 1.png");
/ / Image into the second row the first cell, Zhang Kuange total of six cells
insertImg (imgSheet, 0,1,6,6, imgFile);
workbook.write ();
workbook.close ();
catch (IOException e) (
e.printStackTrace ();
catch (WriteException e) (
e.printStackTrace ();
Support only png format but jxl picture, jpg format and gif format is not supported
================================================== ======
jxl.jar Package Description / java operations excel jxl.jar Download

jxl.jar Package Description Download:
http://www.andykhan.com/jexcelapi/ maximum current version is 2.6.

Real Download:

http://www.andykhan.com/jexcelapi/download.html

On its website features the following description:
● support all versions of Excel 95-2000 ● ● generate standard format for Excel 2000 supports fonts, numbers, date of operation ● ● cell properties can be modified to support images and graphics should be said that the function has been able to largely meet our needs. The key is this is a pure Java API, and does not rely on Windows system, even run Linux, it can correctly handle the same Excel file. Also need to note is that this API support for graphics and charts is limited, and only recognize PNG format.
Built environment will be unpacked after downloading the file, get jxl.jar, into the classpath, installation is complete.
A basic operation, create a file to be generated a "test data. Xls" the Excel file, where the first worksheet is named "first page" results generally are as follows:
Code (CreateXLS.java):
/ / Generate Excel class
import java.io. *;
import jxl .*;
import jxl.write .*;
public class CreateXLS
(
public static void main (String args [])
(
try
(
/ / Open file
WritableWorkbook book =
Workbook.createWorkbook (new File ("test. Xls"));
/ / Build called "first page" the work of the table, the parameter 0 is the first page
WritableSheet sheet = book.createSheet ("First Page", 0);
/ / Constructor in the Label object named in the first column of the cell position is the first line (0,0)
/ / And the cell contents to test
Label label = new Label (0,0, "test");
/ / To be defined cell to worksheet
sheet.addCell (label);
/ * Save the number to generate a cell must use the Number of the full package path, or a grammatical ambiguity cell location is the second column, first row, a value of 789.123 * /
jxl.write.Number number = new jxl.write.Number (1,0,789.123);
sheet.addCell (number);
/ / Write data and close the file
book.write ();
book.close ();
) Catch (Exception e)
(
System.out.println (e);
)
)
)
Compiler implementation, it will produce a current position Excel file.
Third, read the file to the Excel file we created earlier, for example, do a simple read operation, the program code is as follows:
/ / Read Excel class
import java.io. *;
import jxl .*;
public class ReadXLS
(
public static void main (String args [])
(
try
(
Workbook book =
Workbook.getWorkbook (new File ("test. Xls"));
/ / Get the first worksheet object
Sheet sheet = book.getSheet (0);
/ / Get the first column of the first row of cells
Cell cell1 = sheet.getCell (0,0);
String result = cell1.getContents ();
System.out.println (result);
book.close ();
) Catch (Exception e)
(
System.out.println (e);
)
)
)
Program execution results: test
4, modify the file using jExcelAPI can modify an existing Excel file, modify the Excel document, in addition to opening the file in different ways, other operations and create Excel is the same. The following example is generated in the Excel file we have added a worksheet:
/ / Modify Excel class, add a worksheet
import java.io. *;
import jxl .*;
import jxl.write .*;
public class UpdateXLS
(
public static void main (String args [])
(
try
(
/ / Excel access to documents
Workbook wb = Workbook.getWorkbook (new File ("test. Xls"));
/ / Open a file copy, and specify the data to write back to the original file
WritableWorkbook book =
Workbook.createWorkbook (new File ("test. Xls"), wb);
/ / Add a worksheet
WritableSheet sheet = book.createSheet ("second page", 1);
sheet.addCell (new Label (0,0, "the second page of the test data"));
book.write ();
book.close ();
) Catch (Exception e)
(
System.out.println (e);
)
)
)
Implementation of the results shown in Figure:
A high-level operation, the data format in Excel, do not involve complex data types, can better deal with strings, numbers and dates have been able to meet the general application.
1, the formatting string format string is related to the font, weight, size and other elements, these functions mainly responsible for WritableFont and WritableCellFormat class. Suppose we generate a cell contains a string, use the following statement, for the convenience of description, we add a command for each line of code:

WritableFont font1 =
new WritableFont (WritableFont.TIMES, 16, WritableFont.BOLD); or / / set the font format is supported formats excel WritableFont font3 = new WritableFont (WritableFont.createFont ("italics _GB2312"), 12, WritableFont.NO_BOLD); ① WritableCellFormat format1 = new WritableCellFormat (font1); ② Label label = new Label (0,0, "data 4 test", format1) ③ ① which specifies the string format: font TIMES, font size 16, bold display. WritableFont has a very rich sub-structure for use in different situations, jExcelAPI the java-doc list in detail, there is no longer listed. ② Department code used WritableCellFormat class that is very important, which you can specify various properties of the cell, the cell format back there would be more descriptive. ③ Department uses the Label class constructor, specify the kind of format string is given. In WritableCellFormat class, there is a very important way is to specify the alignment of the data, such as the above examples for us, you can specify:

/ / Specify the horizontal alignment center

format1.setAlignment (jxl.format.Alignment.CENTRE);

/ / To specify the vertical alignment center

format1.setVerticalAlignment (jxl.format.VerticalAlignment.CENTRE);

/ / Set word wrap
format1.setWrap (true);

Second, the cell operation
A very important part of Excel is the operation of the cell, such as row height, column width, cell merging, but fortunately jExcelAPI provide such support. The operation is relatively simple, just introduce the following related API.
1, merge cells
WritableSheet.mergeCells (int m, int n, int p, int q);
Effect from the (m, n) to (p, q) all merged cells, such as:
WritableSheet sheet = book.createSheet ("First Page", 0);
/ / Merge in the first column first column first row to the sixth row of all cells
sheet.mergeCells (0,0,5,0);
Can be either horizontal merger can also be vertical. Merged cells can not be merged again, otherwise it will trigger the exception.
2, row height and column width
WritableSheet.setRowView (int i, int height);
Role is to specify the height of the first row i +1, for example:
/ / Set the height of the first line 200
sheet.setRowView (0,200);
WritableSheet.setColumnView (int i, int width);
Role is to specify the column width of the first i +1, for example:
/ / The first column width set to 30
sheet.setColumnView (0,30);
5, the operation picture
public static void write () throws Exception (
WritableWorkbook wwb = Workbook.createWorkbook (new File ("c: / 1.xls"));
WritableSheet ws = wwb.createSheet ("Test Sheet 1", 0);
File file = new File ("C: \ \ jbproject \ \ PVS \ \ WebRoot \ \ weekhit \ \ 1109496996281.png");
WritableImage image = new WritableImage (1, 4, 6, 18, file);
ws.addImage (image);
wwb.write ();
wwb.close ();
)
Very simple and inserted the same way as the cell, but is the parameters become more, WritableImage This class inherits the Draw, just above him as a constructor, the last argument not to say, that the first four parameters of type double , followed by x, y, width, height, note that here the width and height is not the picture width and height, but the picture is to account for the number of the unit cell, because he inherited Draw the type must be double, how to achieve specific which I did not take a closer look:) as anxious time for living, to complete the function, the other after the time to study. Would continue to use the experience to write to you.
Reading:
Reading time is such a thought, first with an input stream (InputStream) are Excel files, then jxl in workbook Workbook by using Sheet from work to get thin sheet, with the Cell may get a worksheet cell.
InputStream-> Workbook-> Sheet-> Cell, get the excel file the cell code:
String path = "c: \ \ excel.xls"; / / Excel file URL
InputStream is = new FileInputStream (path); / / Write to the FileInputStream
jxl.Workbook wb = Workbook.getWorkbook (is); / / be the work of thin
jxl.Sheet st = wb.getSheet (0); / / get a job in the first thin sheet
Cell cell = st.getCell (0,0); / / get the first worksheet cell, the A1
String content = cell.getContents ();// getContents () will be converted to a string of characters in the Cell
wb.close ();// close the workbook
is.close ();// close the input stream

We could Sheet of getCell (x, y) obtained any one cell, x, y, and excel in the corresponding coordinates.
For example, A1 corresponds to (0,0), A2 corresponds to (0,1), D3 corresponding to (3,2). Excel in the coordinates from the A, 1 start, jxl of all is from 0.
You can also Sheet of getRows (), getColumns () method by rows of columns, and for circulation control, output all the contents of a sheet.
Write:
Excel to write the contents of the package is mainly used jxl.write class.
Idea is this:
OutputStream <-WritableWorkbook <-WritableSheet <-Label
That which is written on behalf Label Sheet of Cell position and content.
Code:
OutputStream os = new FileOutputStream ("c: \ \ test.xls ");// output Excel file URL
WritableWorkbook wwb = Workbook.createWorkbook (os); / / create a writable workbook
WritableSheet ws = wwb.createSheet ("sheet1", 0); / / create a write worksheet
Label labelCF = new Label (0, 0, "hello ");// write the location and content creation
ws.addCell (labelCF); / / will write the sheet of Label
Label constructor Label (int x, int y, String aString) xy meaning when read with the xy, aString is the written content.
WritableFont wf = new WritableFont (WritableFont.TIMES, 12, WritableFont.BOLD, false); / / Set the font to write
WritableCellFormat wcfF = new WritableCellFormat (wf); / / set CellFormat
Label labelCF = new Label (0, 0, "hello ");// create write location, content and format
Another constructor Label Label (int c, int r, String cont, CellFormat st) can be written to format the content, set the font and other attributes.
Can now be written
wwb.write ();
After writing close
wwb.close ();
Let the output stream has been closed
os.close;
OK, as long as the read and write together, we can read in the N-Excel, you want the Excel data is written to a new table, it is quite easy.

The following are procedures for example:

Code: sql = "select * from tablename";
rs = stmt.executeQuery (sql);

/ / New Excel File
String filePath = request.getRealPath ("aaa.xls");
File myFilePath = new File (filePath);
if (! myFilePath.exists ())
myFilePath.createNewFile ();
FileWriter resultFile = new FileWriter (myFilePath);
PrintWriter myFile = new PrintWriter (resultFile);
resultFile.close ();

/ / File using JXL to add new content
OutputStream outf = new FileOutputStream (filePath);
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook (outf);
jxl.write.WritableSheet ws = wwb.createSheet ("sheettest", 0);

int i = 0;
int j = 0;

for (int k = 0; k <rs.getMetaData (). getColumnCount (); k + +) (
ws.addCell (new Label (k, 0, rs.getMetaData (). getColumnName (k +1)));
)

while (rs.next ()) (
out.println (rs.getMetaData (). getColumnCount ());

for (int k = 0; k <rs.getMetaData (). getColumnCount (); k + +) (
ws.addCell (new Label (k, j + i +1, rs.getString (k +1)));
)

i + +;
)
wwb.write ();
wwb.close ();
) Catch (Exception e) (e.printStackTrace ();)
finally (

rs.close ();
conn.close ();
)

response.sendRedirect ("aaa.xls");
==============================================
1 / **
2 * QuickExcel.java
3 * author: Yang Qingcheng
4 * Created on 2004 Year 11 22, 4:05 pm
5 * In practice, often to the database table into Excel
6 * I based on Apache's POI to write a simple class
7 * are inappropriate that please correct me, thank you!
8 *
9 * /
10
11package yqc.poi;
12
13import java.sql .*;
14import java.util .*;
15import java.io. *;
16import java.io.ByteArrayInputStream;
17import java.io.FileInputStream;
18import java.io.FileOutputStream;
19import java.io.IOException;
20
21import org.apache.poi.hssf.usermodel .*;
22import org.apache.poi.poifs.filesystem.POIFSFileSystem;
23import org.apache.poi.hssf.record .*;
24import org.apache.poi.hssf.model .*;
25import org.apache.poi.hssf.usermodel .*;
26import org.apache.poi.hssf.util .*; import yqc.sql .*;
27
28 /**//**
29 *
30 * @ author Administrator
31 * /
32public class QuickExcel (
33
34 /**//** Creates a new instance of QuickExcel * /
35 private QuickExcel (String file) (
36 _file = file;
37)
38
39 private void open () throws IOException (
40 InputStream stream = null;
41 Record [] records = null;
42 POIFSFileSystem fs =
43 new POIFSFileSystem (new FileInputStream (_file));
44 _wb = new HSSFWorkbook (fs);
45)
46
47 private void create () (
48 _wb = new HSSFWorkbook ();
49)
Of 50
51 public static QuickExcel newInstance (String file) (
52 QuickExcel qe = new QuickExcel (file);
53 qe.create ();
54 return qe;
55)
56
57 public static QuickExcel openInstance (String file) throws IOException (
58 QuickExcel qe = new QuickExcel (file);
59 qe.open ();
60 return qe;
61)
62
63 public void close () (
64 try (
65 FileOutputStream fileOut = new FileOutputStream (_file);
66 _wb.write (fileOut); / / the Workbook object in the output to a file workbook.xls
67 fileOut.close ();
68)
69 catch (Exception ex) (
70 System.out.println (ex.getMessage ());
71)
72)
73
74 private void removeSheet (String sheetName) (
75 int i = _wb.getSheetIndex ("sheetName");
76 if (i> = 0) _wb.removeSheetAt (i);
77)
78
79 public int fillSheet (ResultSet rs, String sheetName) throws SQLException (
80 HSSFSheet st = _wb.createSheet (sheetName);
81 ResultSetMetaData rsmd = rs.getMetaData ();
82 int index = 0;
83 int result = 0;
84 HSSFRow row = st.createRow (index + +);
85 for (int i = 1; i <= rsmd.getColumnCount ();++ i) (
86 HSSFCell cell = row.createCell ((short) (i-1));
87 cell.setCellValue (rsmd.getColumnName (i));
88)
89 while (rs.next ()) (
90 result + +;
91 row = st.createRow (index + +);
92 for (int i = 1; i <= rsmd.getColumnCount ();++ i) (
93 HSSFCell cell = row.createCell ((short) (i-1));
94 cell.setEncoding (cell.ENCODING_UTF_16);
95 cell.setCellValue (rs.getString (i));
96)
97)
98 return result;
99)
100
101 public static void main (String [] args) (
102 try (
103 QuickConnection qc = new MssqlConnection ("jdbc: microsoft: sqlserver: / / 192.168.0.100:1433; DatabaseName = ls");
104 QuickExcel qe = QuickExcel.newInstance ("a.xls");
105 qc.connect ();
106 String sql = "select * from ls.dbo.radio1_emcee";
107 ResultSet rs = qc.getStatement (). ExecuteQuery (sql);
108 qe.fillSheet (rs, "MT");
109 qe.close ();
110 qe = QuickExcel.openInstance ("a.xls");
111 qe.fillSheet (rs, "MO");
112 qe.close ();
113 qc.close ();
114)
115 catch (SQLException ex) (
116 System.out.println (ex.getMessage ());
117)
118 catch (IOException ex) (
119 System.out.println (ex.getMessage ());
120)
121)
122
123 HSSFWorkbook _wb;
124 String _file = "new.xls";
125)
===================================
/ ** Export data to XLS format
* @ Param fos generate Excel file Path
* @ Param bo the data to be imported
* /
public void writeExcelBo (FileOutputStream fos, java.util.Vector ve)
(
jxl.write.WritableWorkbook wwb;
try
(
wwb = Workbook.createWorkbook (fos);
jxl.write.WritableSheet ws = wwb.createSheet ("booksheet", 10);
ws.addCell (new jxl.write.Label (0, 1, "Bibliography ID"));
ws.addCell (new jxl.write.Label (1, 1, "ISBN"));
ws.addCell (new jxl.write.Label (2, 1, "price"));
ws.addCell (new jxl.write.Label (3, 1, "title"));
ws.addCell (new jxl.write.Label (4, 1, "the original title"));
ws.addCell (new jxl.write.Label (5, 1, "Vice-title"));
ws.addCell (new jxl.write.Label (6, 1, "author"));
ws.addCell (new jxl.write.Label (7, 1, "translator"));
ws.addCell (new jxl.write.Label (8, 1, "edition"));
ws.addCell (new jxl.write.Label (9, 1, "published in"));
ws.addCell (new jxl.write.Label (10, 1, "Press"));
ws.addCell (new jxl.write.Label (11, 1, "Publication Date"));
ws.addCell (new jxl.write.Label (12, 1, "Page"));
ws.addCell (new jxl.write.Label (13, 1, "the book High"));
ws.addCell (new jxl.write.Label (14, 1, "binding"));
ws.addCell (new jxl.write.Label (15, 1, "Series Name"));
ws.addCell (new jxl.write.Label (16, 1, "General Note item"));
ws.addCell (new jxl.write.Label (17, 1, "Introduction"));
ws.addCell (new jxl.write.Label (18, 1, "Subject"));
ws.addCell (new jxl.write.Label (19, 1, "in the classification method"));
ws.addCell (new jxl.write.Label (20, 1, "updated"));
ws.addCell (new jxl.write.Label (21, 1, "the number"));
book = new Book [ve.size ()];
for (int i = 0; i <ve.size (); i + +)
(
book [i] = (Book) ve.get (i);
ws.addCell (new jxl.write.Label (0, i + 2, "" + book [i]. getBookId ()));
ws.addCell (new jxl.write.Label (1, i + 2, book [i]. getIsbn ()));
ws.addCell (new jxl.write.Label (2, i + 2, "" + book [i]. getPrice ()));
ws.addCell (new jxl.write.Label (3, i + 2, book [i]. getBookTitle ()));
ws.addCell (new jxl.write.Label (4, i + 2, book [i]. getOldFilename ()));
ws.addCell (new jxl.write.Label (5, i + 2, book [i]. getSubTitle ()));
ws.addCell (new jxl.write.Label (6, i + 2, book [i]. getWriter ()));
ws.addCell (new jxl.write.Label (7, i + 2, book [i]. getTranscribe ()));
ws.addCell (new jxl.write.Label (8, i + 2, "" + book [i]. getVersion ()));
ws.addCell (new jxl.write.Label (9, i + 2, book [i]. getPublishCity ()));
ws.addCell (new jxl.write.Label (10, i + 2, book [i]. getPublisher ()));
ws.addCell (new jxl.write.Label (11, i + 2, book [i]. getPublishDate (). toString ()));
ws.addCell (new jxl.write.Label (12, i + 2, "" + book [i]. getPage ()));
ws.addCell (new jxl.write.Label (13, i + 2, "" + book [i]. getHight ()));
ws.addCell (new jxl.write.Label (14, i + 2, book [i]. getInstall ()));
ws.addCell (new jxl.write.Label (15, i + 2, book [i]. getSeries ()));
ws.addCell (new jxl.write.Label (16, i + 2, book [i]. getNotes ()));
ws.addCell (new jxl.write.Label (17, i + 2, book [i]. getPrecisnotes ()));
ws.addCell (new jxl.write.Label (18, i + 2, book [i]. getSubject ()));
ws.addCell (new jxl.write.Label (19, i + 2, book [i]. getCls (). replaceAll ("_", "")));
ws.addCell (new jxl.write.Label (20, i + 2, book [i]. getUpdatedate (). toString ()));
ws.addCell (new jxl.write.Label (21, i + 2, "0"));
)
jxl.write.WritableFont wfc =
new jxl.write.WritableFont (
WritableFont.ARIAL,
255,
WritableFont.BOLD,
false,
UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat (wfc);
ws.addCell (new jxl.write.Label (0, 0, "submit your order to ensure stability and correct when importing orders Do not change this table format (do not change the bibliographic ID, order the number of their own to add!)") );
wwb.write ();
/ / Close the Excel workbook object
wwb.close ();
) Catch (IOException e)
() Catch (RowsExceededException e)
() Catch (WriteException e)
()
)

/ / Import EXCEL
if (f.getName (). indexOf (". xls")> 0)
(
try
(
fis = new FileInputStream (f);
BookBean bob = new BookBean ();
UserBean usb = new UserBean ();
jxl.Workbook rwb = Workbook.getWorkbook (fis);
jxl.Sheet sh = rwb.getSheet (0);
int rowCount = sh.getRows ();
SimpleDateFormat sdf = new SimpleDateFormat ("dd / MM / yyyy");
book = new Book [rowCount - 1];
for (int i = 1; i <rowCount; i + +)
(
book [i - 1] = new Book ();
jxl.Cell [] ce = sh.getRow (i);
book [i - 1]. setIsbn (ce [0]. getContents (). toString ());
book [i - 1]. setSeries (ce [1]. getContents (). toString ());
book [i - 1]. setBookTitle (ce [2]. getContents (). toString ());
book [i - 1]. setWriter (ce [3]. getContents (). toString ());
book [i - 1]. setTranscribe (ce [4]. getContents (). toString ());
book [i - 1]. setPublisher (ce [5]. getContents (). toString ());
book [i - 1]. setPublishDate (sdf.parse (ce [6]. getContents (). toString (), new ParsePosition (0)));
book [i-1]. setVersion (Integer.parseInt (ce [7]. getContents (). toString ()));
book [i-1]. setPage (Integer.parseInt (ce [8]. getContents (). toString ()));
book [i-1]. setCls (ce [9]. getContents (). toString ());
book [i-1]. setPrecisnotes (ce [10]. getContents (). toString ());
book [i-1]. setInstall (ce [11]. getContents (). toString ());
book [i-1]. setPrice (Float.parseFloat (ce [12]. getContents (). toString ()));
book [i-1]. setUserid (usb.getUser (). getUserid ());
getVector (). addElement (book [i - 1]);
)
rwb.close ();
fis.close ();
) Catch (FileNotFoundException e)
() Catch (BiffException e)
() Catch (IOException e)
() Catch (NumberFormatException e)
(
ShowMessage ("data import fails, follow the format of the EXCEL software required to import orders");
)
)
=================================================

Document options

Levels:

Rubber (mailto: userid@us.ibm.com? Subject = operating EXCEL file using JAVA & cc = userid@us.ibm.com),

Friends use Windows operating system Excel (spreadsheet) will definitely be familiar, but to use the Java language to manipulate the Excel file is not an easy task. Increasingly popular in Web applications today, to manipulate Excel files through the Web growing demand, now more popular in the JSP or Servlet action is to create a CSV (comma separated values) file and the file to MIME, text / csv type returned to the browser, the browser then calls Excel and show CSV file. This just says access to the Excel file, but it does not really manipulate Excel files, this will give us a pleasant surprise, to tell you about an open source project, Java Excel API, you can easily use it to manipulate Excel files of the.

分类:Java 时间:2010-05-02 人气:1411
分享到:
blog comments powered by Disqus

相关文章

  • java code for the page 2010-02-09

    Recent studies have crawled pages, and discovered to obtain the page's encoding format, Java implementations is not ready, although there are a csdn Daren wrote an article, with code, unfortunately, I did not find the relevant package, a last resort,

  • java code to write excel and text document import and export [change] 2010-03-29

    Example 1 -----" using jxl package to complete the excel import and export JXL package provides a JAVA environment, method of operation of EXCEL files, EXCEL files can read and write operations. Overall, the use of this package is very simple, becaus

  • java code programming optimization 2010-03-26

    Procedures for the use of available resources (memory, CPU time, network bandwidth, etc.) is limited, the purpose of optimization is to allow programs to use the least resources to complete the scheduled tasks. Optimization usually contains two aspec

  • Test java code using 2010-04-16

    package java2html; import java.io.*; /** * Java To HTML code <code><p>5555555555</p></code> * * @author llliiiuuu * @since 2008-12-16 * @version 0.1a */ public class JavaToHTML { public static void main(String[] args) throws IOExce

  • Java code to build a pool of threads 2010-04-25

    In modern operating systems, there is a very important concept - the thread, almost all currently popular operating systems support threads, thread the concept of the process from the operating system, the process has its own virtual address space an

  • java Code Generation 2010-05-01

    1. Generated verification code: using technology jsp, servlet 2. Generated verification code in the servlet: package servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.I

  • Java code optimization, strategies and methods 2010-05-04

    Java code optimization, strategies and methods - Summary 1. How to Use Exception Exception decrease performance. An exception thrown first need to create a new object. Throwable interface, the constructor call, called fillInStackTrace () local method

  • Typical Java code for the thread pool and various parts of Features 2010-05-08

    (1) According to the xml file to manage the thread pool's maximum and minimum number of threads (2) Timer on a regular basis by scanning the thread pool to prevent the thread is not active; (3) by a single variable (in this procedure is freeThreadCou

  • Java code to achieve excel data into Oracle 2010-05-11

    1. First need two jar package jxl.jar, ojdbc.jar (note version, the version will report inappropriate version error) 2. Code: import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import jxl.Cell

  • Java code for a thread pool 2010-07-18

    In modern operating systems, there is a very important concept - the thread, almost all currently popular operating systems support threads, thread the concept of the process from the operating system, the process has its own virtual address space an

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.056 (s). 11 q(s)