It is also a simple import operation for excel. Here, it is just a simple list of the same operation steps as the previous one. the program should explain everything in quite detail. I hope you can understand that it is quite basic and simple. the following code will be expanded to perform Web operations.
- Package com. Lip. Main;
- Import java. Io. file;
- Import java. SQL. connection;
- Import java. SQL. drivermanager;
- Import java. SQL. preparedstatement;
- Import jxl. cell;
- Import jxl. sheet;
- Import jxl. workbook;
- Public class importexcel {
- /**
- * (The most basic import operation .)
- *
- * Author lip (lizg)
- * 2009-1-9
- *
- * The same as the preceding one, this is a simple import operation and is actually the most basic requirement,
- * Add some processing, such as processing your own fields and saving the imported files.
- *
- * Here we also use a main to describe the basic operation process.
- *
- * Import: extract data from Excel and write the data to the database (here is the database operation, which is omitted from other Excel operations)
- *
- * First, prepare the data source first. Instead of creating an Excel file, create an Excel file under the drive letter and import the data.
- *
- * Connect to an example. This operation also applies to MySQL. Of course, it can be replaced with other operations such as Oracle and sqlserver.
- *
- */
- Public static void main (string [] ARGs ){
- // (First, declare the source of the file to be read. Again, it is direct here. I think it is basic but easy to understand.
- File importexcel = new file ("C: // import test .xls ");
- // Check whether this file exists. Of course, you can use file. exists () to check whether it exists,
- /** (Step 1 )*/
- // (Second: connect to the database first. The operation is also performed on the same database, but a new table is added here. See the bottom.
- Try {
- // Database connection
- Class. forname ("com. MySQL. JDBC. Driver"). newinstance ();
- Connection conn = drivermanager. getconnection ("JDBC: mysql: // localhost: 5885/Excel", "root", "5885 ");
- Preparedstatement prep = conn. preparestatement ("insert into importexcel (ID, name) values (?,?) ");
- // This is insert, which is more convenient to use, so you can use this interface directly.
- // System. Out. println ("success ");
- /** (Step 2 )*/
- /**
- * At this point, the database has been connected and uploaded. The SQL statement here is insert, which is very simple. Let's take a look at the data reading.
- *
- */
- // (Step 3: Read the data in an Excel file. The idea is the same,
- // Obtain the Excel file ----> read every unit of work (just like the previous import) ----> Read (a table) the content of each row ------ (read complete .)
- // First, obtain the file, that is, the Excel file above. Here is the Excel file or the file. Then, perform operations on it.
- Workbook workbook = Workbook. getworkbook (importexcel );
- // The second step is to obtain the worksheet of this Excel file. This is basically processed here. Of course, the array format is not used, because only the first worksheet has data, and the other two do not,
- // This operation is still performed for generalization purposes.
- Sheet [] Sheet = Workbook. getsheets ();
- // You can check the API to see which table can be obtained through parameters,
- // For example, Sheet sheet = Workbook. getsheet (1 );
- // This is the final part. The final part here is the final part of the work table. Since it is obtained using arrays, it certainly reads each work table,
- // Here, the read operation reads each row of data in each work table.
- Int sheet_ I _num = 0; // obtain the number of rows in the workbook.
- String id = "";
- String name = ""; // used to obtain the content of each cell, which is used below.
- If (sheet! = NULL & sheet. length> 0) {// judge
- For (INT sheetnum = 0; sheetnum <sheet. length; sheetnum ++) {// obtain the number of work tables for each operation.
- // (3.1 first obtain the number of rows in the workbook to be read .)
- Sheet_ I _num = sheet [sheetnum]. getrows ();
- // The next step is to de-data each row,
- For (INT rownum = 0; rownum <sheet_ I _num; rownum ++ ){
- // Operations on each cell are started here.
- // Apparently, the first parameter of sheet [] is a work table, which row is getrow, and then assigned to the cell for operation.
- Cell [] cells = sheet [sheetnum]. getrow (rownum );
- // The data of each row is read here, and no other judgment is made here, for example, whether the data is an integer, whether it exceeds the string length, and whether it is null,
- Id = cells [0]. getcontents ();
- Name = cells [1]. getcontents (); // here we get each column. Cells assigns a value to a column in this row.
- // The data is obtained and then directly inserted into the database,
- Prep. setint (1, integer. parseint (ID ));
- Prep. setstring (2, name );
- Prep.exe cuteupdate ();
- System. Out. println (ID + "--------" + name );
- }
- }
- }
- Workbook. Close ();
- Prep. Close ();
- Conn. Close ();
- } Catch (exception e ){
- System. Out. println (E. getmessage ());
- }
- Finally {
- }
- }
- }
- /**
- Step 1: create a data table: (The table name is importexcel)
- Drop table if exists 'importexcel ';
- Create Table 'importexcel '(
- 'Id' int (11) not null auto_increment,
- 'Name' varchar (50) default null,
- Primary Key ('id ')
- ) Engine = InnoDB default charset = utf8;
- At this time, there is nothing in it,
- You can use the SELECT statement to check whether or not,
- */
- /*
- Then, we will operate on this data table field. add data to it. Here there are obviously only two fields, the first is ID, (INT Integer type)
- The second is name, (varchar string type)
- Obviously, we can see it.
- Then try again in select.
- */