Java将xlsx或者xls数据读入SQLite数据库

做大创过程中需要将大量Excel数据读入SQLite数据库,百度了很多,但并没有解决我的问题,摸索了一天,想出了一个稍显复杂的办法:

第一步:将xlsx或者xls文件另存为csv文件(注意不能直接修改后缀),如果文件有很多,可以用Excel的宏来批量转换,代码如下:

Sub SaveToCSVs()

    Dim fDir As String

    Dim wB As Workbook

    Dim wS As Worksheet

    Dim fPath As String

    Dim sPath As String

    fPath = "F:\xxx\xxx\"      //存放Excel文件的文件夹

    sPath = "F:\xxx\CSV\"     //将批量转换生成的CSV文件存入sPath文件夹下

    fDir = Dir(fPath)

    Do While (fDir <> "")

        If Right(fDir, 4) = ".xls" Or Right(fDir, 5) = ".xlsx" Then

            On Error Resume Next

            Set wB = Workbooks.Open(fPath & fDir)

            'MsgBox (wB.Name)

            For Each wS In wB.Sheets

                wS.SaveAs sPath & wB.Name & ".csv", xlCSV

            Next wS

            wB.Close False

            Set wB = Nothing

        End If

        fDir = Dir

        On Error GoTo 0

    Loop

End Sub

第二步:将CSV文件的编码方式修改为UTF-8,推荐用EditPlus批量修改文件编码方式,具体操作步骤后续补充。

第三步,利用Java修改CSV文件后缀为txt,代码如下:

package 大创数据导入;

/** 
 * @ClassName: ChangeSuffix
 * @description: 
 * @author: Cyril_KI
 * @Date: 2020年4月26日 上午10:24:55
 */


import java.io.File;
import java.util.Scanner;

public class ChangeSuffix {
	static String path;
	static String from,to;
	public ChangeSuffix(String path,String from,String to) {
		// TODO 自动生成的构造函数存根
		this.path=path;
		this.from=from;
		this.to=to;
	}
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改文件后缀名的文件夹:");
        String path = sc.nextLine();
        System.out.println("请输入修改前的后缀名:");
        //String from = "csv";
        System.out.println("请输入修改后的后缀名:");
        //String to = "txt";

        reName(path, from, to);
        System.out.println("全部修改完成!!!");
    }

    public static void reName(String path, String from, String to) {
        File f = new File(path);
        File[] fs = f.listFiles();
        for (File subFile : fs) {
            // 如果文件是文件夹则递归调用批量更改文件后缀名的函数
            if (subFile.isDirectory()) {
                reName(subFile.getPath(), from, to);
            } else {
                String name = subFile.getName();
                if (name.endsWith(from)) {

                    subFile.renameTo(new File(subFile.getParent() + "/" + name.substring(0, name.indexOf(from)) + to));
                }
            }
        }
    }

}

第四步:读取txt文件存入SQLite数据库,代码后续补充。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章