JAVA implements Excel reading and writing, and JAVA implements Excel reading and writing
Some time ago, due to the needs of website development, I studied how to read and write excel in java. Generally, when we do management software, we need to print reports. How can we make reports? I believe it will be difficult for you. This article will unveil the secrets of this Article. After learning about this article, you will have a deep understanding of report production.
Let's not talk much about it. Next we will begin to summarize this article and generate a variety of excel methods. This article takes the simplest way to use jxl. jar to implement this function as an example to start excel generation.
For the download of jxl. jar, you can collect it online. With this, we can start designing the relevant code.
1. Create a web project and import jxl. jar.
2. Create an excel document:
Import java. io. file; import jxl. workbook; import jxl. write. label; import jxl. write. writableSheet; import jxl. write. writableWorkbook; public class jsl_write {/*** create an excel file write operation * @ param args */public static void main (String [] args) {File file = new File ("E:/cnblogs/jsl_text.xls"); // the location of the generated table store String [] str = {"id", "name ", "sex"}; // Title content try {if (! File. exists () {// checks whether a file exists. createNewFile (); // create a new file} // create a workbook WritableWorkbook Workbook = workbook. createWorkbook (file); // create the sheet object WritableSheet sheet = workbook. createSheet ("sheet1", 0); // row Object Label = null; // Add the title for (int I = 0; I <str. length; I ++) {/*** Label (I, j, String); * I: column * j: row * String: CONTENT */label = new Label (I, 0, str [I]); sheet. addCell (label);} // Add data for (int I = 1; I <= 10; I ++) {label = new Label (0, I, I + ""); sheet. addCell (label); label = new Label (1, I, "name" + I); sheet. addCell (label); label = new Label (2, I, "male"); sheet. addCell (label);} workbook. write (); // write table information to the workbook. close (); // close} catch (Exception e) {e. printStackTrace ();} System. out. println ("Table generation! ");}}
3. Read files in excel:
Import java. io. file; import jxl. cell; import jxl. sheet; import jxl. workbook; public class jsl_reading {public static void main (String [] args) {File file = new File ("E:/cnblogs/jsl_text.xls "); // table store location try {if (file. exists () {// determine whether a file exists // create a Workbook workbook = Workbook. getWorkbook (file); // obtain the first worksheet sheet1 Sheet sheet = workbook. getSheet (0); // get the data for (int I = 0; I <sheet. getRows (); I ++) {// sheet. getRows (): Get the number of rows in the table file for (int j = 0; j <sheet. getColumns (); j ++) {// sheet. getColumns (): Get the number of columns in the table file. Cell cell = sheet. getCell (j, I); System. out. print (cell. getContents () + "");} System. out. println ("");} workbook. close (); // close} else {System. out. println ("file does not exist") ;}} catch (Exception e) {e. printStackTrace ();}}}
Is it very easy? I believe everyone has learned it. Of course, for the function of generating reports on the website, it is actually the process of creating an excel file and downloading it to a local place. If you are interested, please study it. If you have any questions, please leave a message.