Put the code first, run it, look at the effect, then look at the comments, and then look at the basics behind the code.
Create a new Excel and write the data:
public static void MyExcel2 () throws IOException, writeexception {writableworkbook WWB = Workbook.createworkbook (New File ("F:" + file.separator + "Myexcel1.xls"); /Create an sheet entry in an Excel table Writablesheet sheet = wwb.createsheet ("First page", 0);//The cell element in Excel can have many data types, such as Number,label, etc.// In the API, we are provided with a lot of cell type//cell format editing//Merge cell format sheet.mergecells (0, 0, 5, 0);/* =========== format of text in cell case ============ */// The font in the cell element (font format, color, size control) Writablecellformat Forfont = new Writablecellformat ();//Cell style object Writablefont font = new Writablefont (writablefont.arial);//Select Font Font.setcolour (colour.red);//Color font.setpointsize (20); Size Font.setunderlinestyle (underlinestyle.double);//underline forfont.setfont (font); Adds a font style object to the cell style object Forfont.setalignment (alignment.centre);//Text Center//Create a cell and specify location, content and format label Toplabel = new label (0 , 0, "merge cells, set font format", forfont);//Add the Created Cell object into the sheet page Sheet.addcell (toplabel); Sheet.setrowview (0, 1000);/* ========== = formatted case of number in cell ============ *///Create number formatted object NumberFormat Numformat = new NumberFormat ("#.000");//Create cells to format objects and add numeric formatting objects Writablecellformat Fornumber = new Writablecellformat (Numformat); Number MyNumber = new Number (0, 1, 3.14159, Fornumber), Sheet.addcell (MyNumber);/* =========== format case of date in cell ============ * /dateformat DateFormat = new DateFormat ("Yyyy-mm-dd"); Writablecellformat fordate = new Writablecellformat (DateFormat); Fordate.setbackground (Colour.blue); Set cell color DateTime mydate = new DateTime (1, 1, New Date (), fordate); Sheet.addcell (mydate);//write File Wwb.write ();//Close Wwb.clo Se ();}using Java to create a new Excel document, through the Learning API, I summarize a process as follows:
1. You need to create a workbook object that can write, that is, Writableworkbook, and that the passed in parameter can be either a file output stream or an object created through file (the parameter is the file name), such as:
Writableworkbook WWB = newWritableworkbook (New File ("Myexcel.xls"));
2. Create an editable Writablesheet object, you can have multiple sheet pages in an Excel document, and the first sheet is specified as 0, for example:
Writablesheet sheet = wwb.createsheet ("Sheet1", 0);
3. First, create a formatted object of the corresponding type, such as
Nunberformat DateFormat = new Nunberformat ("Yyyy-mm-dd") object
4. Then create an Excel cell to format the object and pass the corresponding type formatting object as a parameter, such as
Writablecellformat Datecellformat = new Writablecellformat (DateFormat);
5. Create a Cell object, set the position, value, and specify the cell formatting object
datetime mydate = new DateTime (0,0,new Date (), Datecellformat);
Parameter description of the cell formatting object: Here is an example of a four-parameter constructor:
The first parameter is the column number, the second argument is the line number, the third parameter is the value, the fourth argument is optional, and the format is represented because the fourth parameter can be specified by the Setcellformat () method
6. Add the Created Cell object to the sheet
7. Write operation: Wwb.write ();
8. Close operation: Wwb.close ()
In particular, if there is an output stream, please close the output stream when not in use.
accidentally found , Java also has number, so we are here to be sure to import jsl.write.Number, otherwise the following statement will be an error:
Number MyNumber = new Number (0, 1, 3.14159, Fornumber);
Date Format parameter Description:
yyyy four-bit year MM Two-bit month DD Two-bit days hh two-bit hour digit mm two-digit minute number (uppercase for month) SS two-bit seconds
Common formats:
Yyyy-mm-dd hh:mm:ss(the middle special symbol can be arbitrarily set, generally can have "-", "/", "")
Parameter description when numbers are formatted:
The format of numbers in Java is mainly used in # and 0 as the position of the characters, explained in detail as follows:
Reserved integer part and 3 decimal places: #.000
Take 2-bit integers and 2 decimal places: 00.00
When the integer part is insufficient, it will be 0, and the number of the missing how many 0
Percentile%: That is, add a% symbol after the above format
#.##% (the "#" function after the decimal point is equivalent to "0")
Scientific notation: With e-letters, such as: 00.000E0
Digital segmentation: Add a "," number in the middle of every three bits
Import Java.text.DecimalFormat; public class Testnumberformat {public static void main (string[] args) {double pi = 3.1415927; PI//Take one-bit integer System.out.println (new DecimalFormat ("0"). Format (pi)); 3//Take one-bit integer and two-bit decimal System.out.println (new DecimalFormat ("0.00"). Format (pi)); 3.14//Take two-bit integers and three-bit decimals, and the integral parts are filled with 0. System.out.println (New DecimalFormat ("00.000"). Format (pi)); 03.142//Take all integer part System.out.println (New DecimalFormat ("#"). Format (pi)); 3//count in percent and take two decimal System.out.println (new DecimalFormat ("#.##%"). Format (pi)); 314.16% long C = 299792458; The speed of light//is shown as scientific notation and takes five decimal System.out.println (new DecimalFormat ("#.#### #E0"). Format (c)); 2.99792E8//Displays the scientific notation for two-bit integers and takes four decimal System.out.println ("00.### #E0"). Format (c)); 29.9792E7//Every three bits are separated by commas. System.out.println (New DecimalFormat (", # # #"). Format (c)); 299,792,458//Embed the format in the text System.out.println (new DecimalFormat ("The speed of light is the size of every second, # # # meters. "). Format (c)); }}
Still, read and modify existing files, as well as common format parameter descriptions