JAVA 讀Excel 表格中的資料.

來源:互聯網
上載者:User

import java.io.File;<br />import java.io.FileInputStream;<br />import java.io.FileNotFoundException;<br />import java.io.IOException;<br />import java.text.SimpleDateFormat;<br />import java.util.Date;</p><p>import org.apache.poi.hssf.usermodel.HSSFCell;<br />import org.apache.poi.hssf.usermodel.HSSFRow;<br />import org.apache.poi.hssf.usermodel.HSSFSheet;<br />import org.apache.poi.hssf.usermodel.HSSFWorkbook;</p><p>public class ImportExcel {</p><p>public static void main(String[] args) {<br />String filePath = "E://kemu.xls";<br />File myFile = new File(filePath);<br />String strAdd = "";<br />try {</p><p>FileInputStream fis = new FileInputStream(myFile);<br />HSSFWorkbook workbook;<br />workbook = new HSSFWorkbook(fis);<br />HSSFSheet sheet = null;<br />HSSFRow row = null;<br />HSSFCell cell = null;<br />SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");<br />String strPrefix = sdf1.format(new Date());<br />if (workbook != null) {<br />sheet = workbook.getSheetAt(0);<br />}<br />if (sheet == null) {<br />System.out.println("不能匯入空的Excel檔案!");<br />}<br />if (sheet != null) {<br />row = sheet.getRow(5);// 從第五行開始讀取<br />}<br />for (int j = 5; row != null; j++, row = sheet.getRow(j)) {<br />strAdd = "";<br />for (int index = 1; index <= 10; index++) {<br />cell = row.getCell((short) (index - 1));<br />if (cell != null) {<br />if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {<br />strAdd = cell.getStringCellValue();<br />}<br />else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {<br />strAdd = String.valueOf(cell.getNumericCellValue());<br />}else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {<br />strAdd = "";<br />}<br />}</p><p>System.out.println(strAdd);<br />}<br />}<br />} catch (FileNotFoundException e) {<br />e.printStackTrace();<br />}catch( IOException ie ){<br />ie.printStackTrace();<br />}</p><p>}</p><p>}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.