In programming, it is often necessary to use tables (reports) for processing.The following shows how to write data to an excel table using java:

1. Add the jar file

Java import and export excel file to import jxl.jar package, the most important thing is that this set of api is pure java, does not rely on the windows system, even if running under linux, it can also correctly handle excel files. Download address:http://www.andykhan.com/jexcelapi/

2.jxl's understanding of excel forms

See also:/go.php?id=68631&s=a

The following figure shows the excel file t.xls to be created. Coordinates (columns, rows):role (0,0), assistant (0,3), functional description (3,0), uc22 (1,4).

3.The java code generates the t.xls file shown in the above picture based on the data in the program

import java.io.file;
import jxl. *;
import jxl.write.label;
import jxl.write.writablesheet;
import jxl.write.writableworkbook;
public class writer_excel {
  public static void main (string [] args) {
    //title line
    string title []={"role", "number", "feature name", "feature description"};
    string context [] []={{"uc11", "Set Course", "Create Course"},              {"uc12", "Set student list", "Give a list of students associated with the course"},              {"uc21", "View student list", ""},              {"uc22", "View group information", "Show information about the group list that the TA is responsible for"}
    //operation execution
    try {
      //t.xls is the name of the file to be created
      writableworkbook book=workbook.createworkbook (new file ("t.xls"));
      //Generate a worksheet named "First Page",Parameter 0 means this is the first page
      writablesheet sheet=book.createsheet ("First page", 0);
      //Write content
      for (int i=0;i&4;i ++) //title
        sheet.addcell (new label (i, 0, title [i]));
      for (int i=0;i<4;i ++) //context
        for (int j=0;j<3;j ++)
          sheet.addcell (new label (j + 1, i + 1, context [i] [j]));
      sheet.addcell (new label (0,1, "teacher"));
      sheet.addcell (new label (0,3, "TA"));
      /*Merge Cells.
Mergers can be horizontal,Can also be vertical
       * writablesheet.mergecells (int m, int n, int p, int q);represents a merge of rectangular areas composed of cells from (m, n) to (p, q)
       * * /
      sheet.mergecells (0,1,0,2);
      sheet.mergecells (0,3,0,4);
      //data input
      book.write ();
      //Close the file
      book.close ();
    catch (exception e) {}
  • Previous On C ++ Object Composition
  • Next Detailed CSTRINGLIST usage in C ++