Import java.io.file;//Introduction Class
Import java.io.IOException;
Import Java.util.Scanner;
Import JXL. Cell;
Import JXL. Sheet;
Import JXL. Workbook;
Import Jxl.format.UnderlineStyle;
Import Jxl.write.DateFormat;
Import Jxl.write.DateTime;
Import Jxl.write.Label;
Import Jxl.write.NumberFormat;
Import Jxl.write.WritableCellFormat;
Import Jxl.write.WritableFont;
Import Jxl.write.WritableSheet;
Import Jxl.write.WritableWorkbook;
public class Textoperatorexcel {//classes that manipulate Excel files
/**
* Generates an Excel file
*/
public static void Writeexcel (String fileName) {
Writableworkbook WWB = null;try {
//Create a writable Workbook (Workbook) object
WWB = Workbook.createworkbook (new File);
} catch (IOException e) {//Capture Stream exception
E.printstacktrace ();
}
if (WWB! = null) {
//Create a writable worksheet
//Workbook The Createsheet method has two parameters, the first is the name of the worksheet, and the second is the position of the worksheet in the workbook
Writablesheet ws = Wwb.createsheet ("Sheet1", 0);
for (int i = 0; i < i++) {//Loop add cell
for (int j = 0; J < 5; J + +) {
Label LABELC = new label (j, I, "This is the "+ (i + 1) +" line, section "
+ (j + 1) +" column ");
try {
Ws.addcell (LABELC);//Add the resulting cell to the worksheet
} catch (Exception e) {//catch exception
E.printstacktrace ();
}
}
}
try {
Wwb.write ();//write from memory to file
Wwb.close ();//write from memory to file
} catch (Exception e) {//catch exception
E.printstacktrace ();
}
}
System.out.println ("Generate an Excel file:" +filename+ "Success!");
}
/**
* Write content to
* @param fileName
* @throws Exception
*/
public static void Writecontenttoexcel (String fileName) throws exception{
File Tempfile=new file (fileName);
Writableworkbook workbook = Workbook.createworkbook (tempfile);
Writablesheet sheet = workbook.createsheet ("Testcreateexcel", 0);
Some temporary variables that are used to write to Excel
Label L=null;
Jxl.write.Number N=null;
Jxl.write.DateTime D=null;
Some of the predefined fonts and formats that are best in the same Excel do not have too many formatting glyph sizes bold Italic underline color
Writablefont HeaderFont = new Writablefont (Writablefont.arial, Writablefont.bold, False, Underlinestyle.no_ Underline, Jxl.format.Colour.BLUE);
Writablecellformat Headerformat = new Writablecellformat (HeaderFont);
Writablefont Titlefont = new Writablefont (writablefont.arial, ten, Writablefont.no_bold, False, Underlinestyle.no_ Underline, Jxl.format.Colour.RED);
Writablecellformat TitleFormat = new Writablecellformat (Titlefont);
Writablefont Detfont = new Writablefont (writablefont.arial, ten, Writablefont.no_bold, False, Underlinestyle.no_ Underline, Jxl.format.Colour.BLACK);
Writablecellformat Detformat = new Writablecellformat (Detfont);
NumberFormat nf=new NumberFormat ("0.00000"); The format used for number
Writablecellformat Priceformat = new Writablecellformat (Detfont, NF);
DateFormat df=new DateFormat ("Yyyy-mm-dd");//For date
Writablecellformat DateFormat = new Writablecellformat (Detfont, DF);
L=new Label (0, 0, "Excel file for testing", headerformat);//create some cells and add to sheet
Sheet.addcell (l);
Add title
int column=0;
L=new Label (column++, 2, "title", TitleFormat);
Sheet.addcell (l);
L=new Label (column++, 2, "date", TitleFormat);
Sheet.addcell (l);
L=new Label (column++, 2, "Currency", TitleFormat);
Sheet.addcell (l);
L=new Label (column++, 2, "price", TitleFormat);
Sheet.addcell (l);
Add content
int i=0;
column=0;
L=new Label (column++, i+3, "title" +i, Detformat);
Sheet.addcell (l);
D=new DateTime (column++, i+3, New Java.util.Date (), DateFormat);
Sheet.addcell (d);
L=new Label (column++, i+3, "CNY", Detformat);
Sheet.addcell (l);
N=new Jxl.write.Number (column++, i+3, 5.678, Priceformat);
Sheet.addcell (n);
i++;
column=0;
L=new Label (column++, i+3, "title" +i, Detformat);
Sheet.addcell (l);
D=new DateTime (column++, i+3, New Java.util.Date (), DateFormat);
Sheet.addcell (d);
L=new Label (column++, i+3, "SGD", Detformat);
Sheet.addcell (l);
N=new Jxl.write.Number (column++, i+3, 98832, Priceformat);
Sheet.addcell (n);
Set the width of a column
column=0;
Sheet.setcolumnview (column++, 20);
Sheet.setcolumnview (column++, 20);
Sheet.setcolumnview (column++, 10);
Sheet.setcolumnview (column++, 20);
Workbook.write ();
Workbook.close ();
SYSTEM.OUT.PRINTLN ("Content write" +filename+ "success");
}
public static void Readexcelinfo (String fileName)
Throws Exception {//How many rows to get the Excel file
Workbook book = Workbook.getworkbook (new File);//construct Workbook (Workbook) object
Sheet Sheet = book.getsheet (0);
Get the first column of the first row of cells//Get first Sheet object
int columnum = Sheet.getcolumns (); Get the number of columns
int rownum = Sheet.getrows (); Get the number of rows
System.out.println (Columnum);
System.out.println (rownum);
for (int i = 0; i < rownum; i++)//loop for read/write
{
for (int j = 0; J < Columnum; J + +) {
Cell cell1 = Sheet.getcell (j, I);
String result = Cell1.getcontents ();
System.out.print (result);
System.out.print ("\ t");
}
System.out.println ();
}
Book.close ();//Close (Workbook) object
}
public static void Main (string[] args) {//Java program main entrance
try {
System.out.println ("1. Create an Excel file, enter the Excel file name (including the path and suffix)");
Scanner scan = new Scanner (system.in);
Final String fileName = Scan.next ();//Get keyboard value
Writeexcel (fileName);//call to generate an Excel method
System.out.println ("2. Write content to Excel file");
Writecontenttoexcel (fileName);//Call to write content to the Excel method
System.out.println ("3. Read Excel file");
Readexcelinfo (FileName);
} catch (Exception e) {//catch exception
E.printstacktrace ();
}
}
}
Java Operations on Excel tables