javaxlsx,javaxlsx批量轉pdf

本文目錄一覽:

java中怎麼獲取所有後綴為.xlsx的路徑

File file = new File(“文件夾路徑”);

File[] xlsxList = file.listFiles((dir, name)-name.endsWith(“.xlsx”));

java poi操作xlsx文件,如何在原xlsx上進行某一行的複製到當前xlsx的新增行,內容和樣式和原行保持不變

首先要讀取xlsx文件,創建一個基於硬碟的FileItem工廠  ,DiskFileItemFactory

用poi讀文件內容,獲取到表格的行,不知道有沒有直接複製某行的方法,但最笨的方法,可以讀取每一個單元格,再新增一行,將數據一次插入

HSSFSheet sheet = workbook.getSheetAt(0);先創建一個sheet對象

HSSFRow row =sheet.getRow(0);以第一行為例

short  lenth=row.getLastCellNum() ; 獲取列數

遍歷列 ,用row.getCell() 獲取列值

創建新行  HSSFRow newRow = sheet.createRow(sheet.getLastRowNum()+1);

再遍歷賦值  HSSFCell cell = newRow.createCell(i);

之前只做過poi的導入導出,所以沒有實測,你可以嘗試一下

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;

    }

}

java excel 解析 什麼開源工具好?

推薦使用poi,這是用的最多的,導入導出都非常方便。

JAVA 使用POI製作表格,而且都是開源的。POI是Apace公司開發的,對中文的支持比較弱一些;而JExcelAPI是韓國公司開發的,不僅對中文的支持好,而且由於是純JAVA編寫的,所以可以跨平台操作。

HSSFCell cell = row.createCell((short) 0);

//設置此單元格的格式為文本,此句可以省略,Excel會自動識別。

//其他還有幾種常用的格式,請參考本文底部的補充部分。

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

//此處是3.0.1版的改進之處,上一版可以直接setCellValue(“Hello, World!”),

//但是在3.0.1里,被deprecated了。

cell.setCellValue(new HSSFRichTextString(“Hello, World!”));

//創建一個文件輸出流,指定到C盤根目錄下(C盤都有吧?)

//xls是Excel97-2003的標準擴展名,2007是xlsx,目前的POI能直接生產的還是xls格式,

//如果此處把擴展名改成xlsx,在用Excel2007打開此文件時會報錯。

小技巧,快捷操作:可以用滑鼠左鍵選中poi-3.0.1-FINAL-20070705.jar但不鬆開,拖到任務欄的Eclipse圖標上等候1秒左右,Eclipse會自動彈起來,依然不鬆開移動到lib文件夾上,這個時候滑鼠後面跟個十字元號,鬆開左鍵,就完成了複製動作。這個是對整個windows系統都好用的快捷複製方式,視源盤符和目標盤符的不同偶爾會用到Ctrl鍵。

java操作xlsx讀出來的數據帶E,怎麼解決

可以轉換下,例如:

String s = “5.05993595E9”;

double d = Double.parseDouble(s);

System.out.println(d);

long l = (long)d;

System.out.println(l);

java怎麼把xls格式的文件另存為xlsx文件,不能直接改後綴名?

一般操作Excel有專業的工具庫來實現,操作時,會考慮同時兼容不同版本的excel,你這裡將xls轉為xlsx,就是版本之間轉換,可以參考以下代碼的轉換方法,方法還是比較簡單,直接將文件另存為就可以了:

import com.spire.xls.*;

public class ExcelConversion {

public static void main(String[] args) {

Workbook wb = new Workbook();

wb.loadFromFile(“test.xls”);

wb.saveToFile(“toXLSX.xlsx”);

}

}

這裡代碼編譯環境為IntelliJ IDEA,jdk是1.8.0版本,使用Excel庫free spire.xls.jar 3.9.1。

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

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

相關推薦

  • Python基礎教程第三版PDF下載

    熟練掌握Python編程語言可以讓你輕鬆地用代碼解決很多問題,Python基礎教程第三版是一本適合初學者的Python教程。本文將從幾個方面詳細介紹Python基礎教程第三版PDF…

    編程 2025-04-29
  • 使用Spire.PDF進行PDF文檔處理

    Spire.PDF是一款C#的PDF庫,它可以幫助開發者快速、簡便地處理PDF文檔。本篇文章將會介紹Spire.PDF庫的一些基本用法和常見功能。 一、PDF文檔創建 創建PDF文…

    編程 2025-04-29
  • Python零基礎PDF下載

    本文將為大家介紹如何使用Python下載PDF文件,適合初學者上手實踐。 一、安裝必要的庫 在Python中,我們需要使用urllib和requests庫來獲取PDF文件的鏈接,並…

    編程 2025-04-29
  • 智能風控 Python金融風險PDF

    在金融交易領域,風險控制是一項重要任務。智能風控是指通過人工智慧技術和演算法模型,對金融交易進行風險識別、風險預警、風險控制等操作。Python是一種流行的編程語言,具有方便、易用、…

    編程 2025-04-29
  • Python編程與數據分析應用PDF

    Python編程是一門功能強大的編程語言,其易讀易寫、可擴展性強等優點使得它在各個領域都有著廣泛的應用。而數據分析也是當今各行各業的基本需求,Python語言通過優秀的數據分析庫也…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • 文本數據挖掘與Python應用PDF

    本文將介紹如何使用Python進行文本數據挖掘,並將著重介紹如何應用PDF文件進行數據挖掘。 一、Python與文本數據挖掘 Python是一種高級編程語言,具有簡單易學、代碼可讀…

    編程 2025-04-28
  • Java批量執行SQL時Communications Link Failure Socket is Closed問題解決辦法

    對於Java開發人員來說,批量執行SQL是一個經常會遇到的問題。但是,有時候我們會遇到「Communications link failure socket is closed」這…

    編程 2025-04-28
  • Python生成PDF文檔

    Python是一門廣泛使用的高級編程語言,它可以應用於各種領域,包括Web開發、數據分析、人工智慧等。在這些領域的應用中,有很多需要生成PDF文檔的需求。Python有很多第三方庫…

    編程 2025-04-28
  • 使用Python為PDF添加書籤

    Python是一種強大靈活的編程語言,它支持大量的庫和模塊,其中就包括pdf模塊。使用Python處理PDF文件可以有效地提高處理效率和減輕工作量。其中,添加書籤是PDF處理的常見…

    編程 2025-04-28

發表回復

登錄後才能評論