javaexcel,java excel導入導出

本文目錄一覽:

如何在Java中操作Excel

POI對excel的支持算是好的了。

但也沒有你說的那樣列印,預覽,功能。

如果是應用程序,不是很好處理。

如果是web程序,可以如下處理。

1.在後台用POI類庫生成excel。

2.用流的方式,將生成的excel傳遞給前台瀏覽器,在瀏覽器中打開。

3.使用瀏覽器的列印功能來進行列印。

Java操作excel的問題

Java創建excel表格的基本步驟:

1.創建一個WritableWorkbook對象(用Workbook的createWorkbook方法創建),要指定創建一個文件;

2.創建一個工作表WritableSheet(用workbook對象的createSheet方法創建),注意要是WritableSheet,說明可以對其寫;

3.創建單元格,再將單元格加入到sheet里;

4.執行workbook的write()方法進行寫操作最後關閉workbook。

Java對Excel解析(求助)

這篇blog主要是講述java中poi讀取excel,而excel的版本包括:2003-2007和2010兩個版本, 即excel的後綴名為:xls和xlsx。

讀取excel和MySQL相關: java的poi技術讀取Excel數據到MySQL

代碼如下

/**

 * 

 */

package com.b510.common;

/**

 * @author Hongten

 * @created 2014-5-21

 */

public class Common {

    public static final String OFFICE_EXCEL_2003_POSTFIX = “xls”;

    public static final String OFFICE_EXCEL_2010_POSTFIX = “xlsx”;

    public static final String EMPTY = “”;

    public static final String POINT = “.”;

    public static final String LIB_PATH = “lib”;

    public static final String STUDENT_INFO_XLS_PATH = LIB_PATH + “/student_info” + POINT + OFFICE_EXCEL_2003_POSTFIX;

    public static final String STUDENT_INFO_XLSX_PATH = LIB_PATH + “/student_info” + POINT + OFFICE_EXCEL_2010_POSTFIX;

    public static final String NOT_EXCEL_FILE = ” : Not the Excel file!”;

    public static final String PROCESSING = “Processing…”;

}

/**

 * 

 */

package com.b510.excel;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

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.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.b510.common.Common;

import com.b510.excel.util.Util;

import com.b510.excel.vo.Student;

/**

 * @author Hongten

 * @created 2014-5-20

 */

public class ReadExcel {

    

    /**

     * read the Excel file

     * @param path the path of the Excel file

     * @return

     * @throws IOException

     */

    public ListStudent readExcel(String path) throws IOException {

        if (path == null || Common.EMPTY.equals(path)) {

            return null;

        } else {

            String postfix = Util.getPostfix(path);

            if (!Common.EMPTY.equals(postfix)) {

                if (Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {

                    return readXls(path);

                } else if (Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {

                    return readXlsx(path);

                }

            } else {

                System.out.println(path + Common.NOT_EXCEL_FILE);

            }

        }

        return null;

    }

    /**

     * Read the Excel 2010

     * @param path the path of the excel file

     * @return

     * @throws IOException

     */

    public ListStudent readXlsx(String path) throws IOException {

        System.out.println(Common.PROCESSING + path);

        InputStream is = new FileInputStream(path);

        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);

        Student student = null;

        ListStudent list = new ArrayListStudent();

        // Read the Sheet

        for (int numSheet = 0; numSheet  xssfWorkbook.getNumberOfSheets(); numSheet++) {

            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);

            if (xssfSheet == null) {

                continue;

            }

            // Read the Row

            for (int rowNum = 1; rowNum = xssfSheet.getLastRowNum(); rowNum++) {

                XSSFRow xssfRow = xssfSheet.getRow(rowNum);

                if (xssfRow != null) {

                    student = new Student();

                    XSSFCell no = xssfRow.getCell(0);

                    XSSFCell name = xssfRow.getCell(1);

                    XSSFCell age = xssfRow.getCell(2);

                    XSSFCell score = xssfRow.getCell(3);

                    student.setNo(getValue(no));

                    student.setName(getValue(name));

                    student.setAge(getValue(age));

                    student.setScore(Float.valueOf(getValue(score)));

                    list.add(student);

                }

            }

        }

        return list;

    }

    /**

     * Read the Excel 2003-2007

     * @param path the path of the Excel

     * @return

     * @throws IOException

     */

    public ListStudent readXls(String path) throws IOException {

        System.out.println(Common.PROCESSING + path);

        InputStream is = new FileInputStream(path);

        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

        Student student = null;

        ListStudent list = new ArrayListStudent();

        // Read the Sheet

        for (int numSheet = 0; numSheet  hssfWorkbook.getNumberOfSheets(); numSheet++) {

            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);

            if (hssfSheet == null) {

                continue;

            }

            // Read the Row

            for (int rowNum = 1; rowNum = hssfSheet.getLastRowNum(); rowNum++) {

                HSSFRow hssfRow = hssfSheet.getRow(rowNum);

                if (hssfRow != null) {

                    student = new Student();

                    HSSFCell no = hssfRow.getCell(0);

                    HSSFCell name = hssfRow.getCell(1);

                    HSSFCell age = hssfRow.getCell(2);

                    HSSFCell score = hssfRow.getCell(3);

                    student.setNo(getValue(no));

                    student.setName(getValue(name));

                    student.setAge(getValue(age));

                    student.setScore(Float.valueOf(getValue(score)));

                    list.add(student);

                }

            }

        }

        return list;

    }

    @SuppressWarnings(“static-access”)

    private String getValue(XSSFCell xssfRow) {

        if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {

            return String.valueOf(xssfRow.getBooleanCellValue());

        } else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {

            return String.valueOf(xssfRow.getNumericCellValue());

        } else {

            return String.valueOf(xssfRow.getStringCellValue());

        }

    }

    @SuppressWarnings(“static-access”)

    private String getValue(HSSFCell hssfCell) {

        if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {

            return String.valueOf(hssfCell.getBooleanCellValue());

        } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {

            return String.valueOf(hssfCell.getNumericCellValue());

        } else {

            return String.valueOf(hssfCell.getStringCellValue());

        }

    }

}

/**

 * 

 */

package com.b510.excel.client;

import java.io.IOException;

import java.util.List;

import com.b510.common.Common;

import com.b510.excel.ReadExcel;

import com.b510.excel.vo.Student;

/**

 * @author Hongten

 * @created 2014-5-21

 */

public class Client {

    public static void main(String[] args) throws IOException {

        String excel2003_2007 = Common.STUDENT_INFO_XLS_PATH;

        String excel2010 = Common.STUDENT_INFO_XLSX_PATH;

        // read the 2003-2007 excel

        ListStudent list = new ReadExcel().readExcel(excel2003_2007);

        if (list != null) {

            for (Student student : list) {

                System.out.println(“No. : ” + student.getNo() + “, name : ” + student.getName() + “, age : ” + student.getAge() + “, score : ” + student.getScore());

            }

        }

        System.out.println(“======================================”);

        // read the 2010 excel

        ListStudent list1 = new ReadExcel().readExcel(excel2010);

        if (list1 != null) {

            for (Student student : list1) {

                System.out.println(“No. : ” + student.getNo() + “, name : ” + student.getName() + “, age : ” + student.getAge() + “, score : ” + student.getScore());

            }

        }

    }

}

/**

 * 

 */

package com.b510.excel.util;

import com.b510.common.Common;

/**

 * @author Hongten

 * @created 2014-5-21

 */

public class Util {

    /**

     * get postfix of the path

     * @param path

     * @return

     */

    public static String getPostfix(String path) {

        if (path == null || Common.EMPTY.equals(path.trim())) {

            return Common.EMPTY;

        }

        if (path.contains(Common.POINT)) {

            return path.substring(path.lastIndexOf(Common.POINT) + 1, path.length());

        }

        return Common.EMPTY;

    }

}

/**

 * 

 */

package com.b510.excel.vo;

/**

 * Student

 * 

 * @author Hongten

 * @created 2014-5-18

 */

public class Student {

    /**

     * id   

     */

    private Integer id;

    /**

     * 學號

     */

    private String no;

    /**

     * 姓名

     */

    private String name;

    /**

     * 學院

     */

    private String age;

    /**

     * 成績

     */

    private float score;

    public Integer getId() {

        return id;

    }

    public void setId(Integer id) {

        this.id = id;

    }

    public String getNo() {

        return no;

    }

    public void setNo(String no) {

        this.no = no;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getAge() {

        return age;

    }

    public void setAge(String age) {

        this.age = age;

    }

    public float getScore() {

        return score;

    }

    public void setScore(float score) {

        this.score = score;

    }

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/297900.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-28 12:17
下一篇 2024-12-28 12:17

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29
  • Java 8 Group By 會影響排序嗎?

    是的,Java 8中的Group By會對排序產生影響。本文將從多個方面探討Group By對排序的影響。 一、Group By的概述 Group By是SQL中的一種常見操作,它…

    編程 2025-04-29

發表回復

登錄後才能評論