/*
Remember to import the third jar package before using
This is what I used to do when someone else's things have been modified for a while before you could properly encapsulate
I'm sorry, I feel like I'm writing it, I'm not going to explain the Code.
*/
Package Com.zzp.ExcelParse;
Import Jxl. Workbook;
Import jxl.format.*;
Import Jxl.format.Alignment;
Import Jxl.format.Border;
Import Jxl.format.BorderLineStyle;
Import jxl.write.*;
Import Jxl.write.Colour;
Import Java.io.FileOutputStream;
Import Java.io.OutputStream;
Import Java.util.List;
/**
* Save the data to an Excel file
*
* Created by Hasee on 2016/11/28.
*/
public class Exceloperaation {
public voidWriteexcel (String Path, List<string[]> List, String sheet, string[] Title) {
try {
Create an Excel workbook
Writableworkbook WWB =Null
Create a new JXL file
OutputStream OS =New FileOutputStream (path);
WWB = Workbook.Createworkbook (OS);
Add the first worksheet and set the first sheet name
Writablesheet sheets = Wwb.createsheet (sheet,1);
Label label;
for (int i =0; I < Title.Length; I++) {
Label (x, y, Z) represents the Cell's x+1 column, line y+1, content Z
Indicates the location and contents of a cell in a child object of a Label object
Label = new label (i, 0, title[i]);
Label =New Label (i,0, title[i],GetHeader ());
Set column widths
Sheets.setcolumnview (i,20);
Sheets.setcolumnview (4, 100);
Add a defined cell to a worksheet
Sheets.addcell (label);
}
Set cell Properties
Writablecellformat WC =New Writablecellformat ();
Set Center
Wc.setalignment (Alignment.CENTRE);
Set Border lines
Wc.setborder (Border.All, Borderlinestyle.THIN);
for (int i =0; I < list.size (); I++) {
string[] arrdata = List.get (i);
for (Int J =0; J < Arrdata.Length; J + +) {
Write data to a specific cell
Sheets.setcolumnview (j, 20);
Label =New Label (j,1 + I, arrdata[j], Wc);
Sheets.addcell (label);
}
}
Write Data
Wwb.write ();
Close File
Wwb.close ();
}Catch (Exception E) {
E.printstacktrace ();
return;
}
}
public static WritablecellformatGetHeader () {
Defining fonts
Writablefont Font =New Writablefont (writablefont.Times,10,
Writablefont.BOLD);
try {
Black Font
Font.setcolour (jxl.format.Colour.BLACK);
}Catch (writeexception E1) {
E1.printstacktrace ();
}
Writablecellformat format =New Writablecellformat (font);
try {
Center left and right
Format.setalignment (jxl.format.Alignment.CENTRE);
Center up and down
Format.setverticalalignment (jxl.format.VerticalAlignment.CENTRE);
Black border
Format.setborder (Border.All, Borderlinestyle. THIN, Jxl.format.Colour. BLACK);
//yellow Background
Format.setbackground (jxl.format.Colour. YELLOW);
} catch (writeexception e) {
E.printstacktrace ();
}
return format;
}
}
Java writes data to an Excel file