Java reads Excel files without dependency, and java reads excel files with dependency

Source: Internet
Author: User

Java reads Excel files without dependency, and java reads excel files with dependency
Speaking of Java reading Excel files, it is of course a lot of use POI or jxls, but when I read a book today, it is mentioned that the JdbcOdbcDriver driver class can also be used to read Excel files without relying on third-party libraries. I searched the internet for the reason and wrote an example and ran through, record it here.

Java is familiar with reading databases and requires a data source and a corresponding driver. developers use JDBC to operate the driver and then operate the database. Therefore, reading Excel files in Java is similar, in Windows, you can register an Excel file as an ODBC Data source. The registration process is as follows:
A. Control Panel> Administrative Tools> data source (ODBC)

B. Click Add on the user DSN tab and select Driver do Microsoft Excel (*. xls)


C. Click "finish", enter the data source name, and then select the Excel file address as the data source.
The data source name is the most important. It is used as part of the URL to obtain the connection.

After registering the data source, you can write code. An example is as follows:

Public class ExcelReader {private String entry; // ODBC Data Source Name public ExcelReader (String entry) {this. entry = entry;} // sheetName is the worksheet name public String read (String sheetName) throws Exception {StringBuilder builder = new StringBuilder (); Class. forName ("sun. jdbc. odbc. jdbcOdbcDriver "). newInstance (); Connection connection = DriverManager. getConnection ("jdbc: odbc:" + entry); Statement statement = connection. createStatement (); ResultSet rs = statement.exe cuteQuery ("select * from [" + sheetName + "$]"); while (rs. next () {builder. append (rs. getObject (1); // read only one column} rs. close (); statement. close (); connection. close (); return builder. toString ();} public static void main (String [] args) throws Exception {ExcelReader reader = new ExcelReader ("etl"); System. out. println (reader. read ("test "));}}

The principle is: the Development uses JDBC to operate the JdbcOdbcDriver driver, the JdbcOdbcDriver driver to connect to the ODBC data source, and then the ODBC operation connects to the Excel file (completed in Windows ), by step-by-step adaptation, you can read Excel files.


Java reads Excel files

Poi itself does not read when there is no content in cells. That is to say, a row has six cells with content. If there is no content in the seventh row and NO content in the future, the row length is 6, you cannot read the seventh one at all, so you do not have to make your own judgment.

If there are six cells with no content, but the other cells still have content when they read the remaining six cells.

An issue of reading excel files in java

Package com. sidi. oa. util;

Import java. io. File;
Import java. io. FileInputStream;
Import java. io. IOException;

Import org. apache. poi. hssf. usermodel. HSSFCell;
Import org. apache. poi. hssf. usermodel. HSSFRow;
Import org. apache. poi. hssf. usermodel. HSSFSheet;
Import org. apache. poi. hssf. usermodel. HSSFWorkbook;
Import org. apache. poi. poifs. filesystem. POIFSFileSystem;

/**
* @ Author (copyright belongs to the original author)
* Used to read excel
*/
Public class ExcelReader {
Private HSSFWorkbook wb = null; // book [partition des sheet]

Private HSSFSheet sheet = null;

Private HSSFRow row = null;

Private int sheetNum = 0; // worksheet No. sheetnum

Private int rowNum = 0;

Private FileInputStream FCM = null;

Private File file = null;

Public ExcelReader (){
}

Public ExcelReader (File file ){
This. file = file;
}

Public void setRowNum (int rowNum ){
This. rowNum = rowNum;
}

Public void setSheetNum (int sheetNum ){
This. sheetNum = sheetNum;
}

Public void setFile (File file ){
This. file = file;
}

/**
* Read the excel file to obtain the HSSFWorkbook object
*/
Public void open () throws IOException {
FS = new FileInputStream (file );
Wb = new HSSFWorkbook (new POIFSFileSystem (FCM ));
FCM. close ();
}

/**
* Returns the number of sheet tables.
*
* @ Return int
*/
Public int getSheetCount (){
Int sheetCount =-1;
SheetCount = wb. getNumberOfSheets ();
Return sheetCount;
}

/**
* Number of records under sheetNum
*
* @ Return int
*/
Public int getRowCount (){
If (wb = null)
System. out. println ("=====================...... the remaining full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.