Java generates Excel and reads Excel example

Source: Internet
Author: User

There are many ways to manipulate Excel using Java, and my http://blog.csdn.net/qq_20545159/article/details/45132041 price has been The following is a simple code that uses JXL to generate an XLS format for Excel.

using JXL to generate Excel files You must first add the Jxl.jar package to the path of your project.

Package com.silence.excel;
Import Java.io.File;

Import JXL. Workbook;
Import Jxl.write.Label;
Import Jxl.write.WritableSheet;
Import Jxl.write.WritableWorkbook;

public class Jxlexcel {

public static void Main (string[] args) {
Set row labels for this table
String[] title = {"id", "name", "Age", "sex", "number"};
Create a file
File File = new file ("E:\\temp.xls");
try {
if (!file.exists ()) {
File.createnewfile ();
}
Create a work
Writableworkbook workbook = workbook.createworkbook (file);
Create sheet
Writablesheet sheet = workbook.createsheet ("Sheet0", 0);
Label label = NULL;
Set a row column name
for (int i = 0; i < title.length; i++) {
Label = new label (i, 0, title[i]);
Sheet.addcell (label);
}
Loop inserts data into each row
for (int i = 0; i < i++) {
Label = new label (0, I, "user" + i);
Sheet.addcell (label);
Label = new label (1, I, "Zhang San" + i);
Sheet.addcell (label);
Label = new label (2, I, +i+ "");
Sheet.addcell (label);
Label = new label (3, I, "a" + i);
Sheet.addcell (label);
Label = new label (4, I, "silencewaking in the Sun" + i);
Sheet.addcell (label);

}

End

Workbook.write ();
Workbook.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}

}

Java Read Excel
Package com.silence.excel;


Import Java.io.File;

Import java.io.IOException;

Import JXL. Cell;
Import JXL. Sheet;
Import JXL. Workbook;
Import jxl.read.biff.BiffException;

public class Jxlreadexcel {

public static void Main (string[] args) {

try {
Create Workbook
Workbook Workbook = Workbook.getworkbook (New File ("E:\\temp.xls"));
GET worksheet sheet
Sheet Sheet = workbook.getsheet (0);
for (int i = 0; i < sheet.getrows (); i++) {
for (int j = 0; J < Sheet.getcolumns (); j + +) {
Cell cell = Sheet.getcell (j,i);
System.out.print (cell.getcontents () + "");
}
System.out.println ();
}
Get Data
Workbook.close ();
} catch (Biffexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}

}




Java generates Excel and reads Excel example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.