2

これに役立つ良い投稿がインターネット上で見つかりませんでした。

私の要件は、スプレッドシートから各行を読み取り、セルの値を使用して SQL ステートメントを生成し、スプレッドシートの読み取りが完了したらバッチ アップロードを行うことです。

私は Apache POI、Spring フレームワーク、および JDBC を使用しています。

Excel から SQL を生成するにはどうすればよいですか?

  1. 引数 (?) を含む SQL ステートメントがあり、セルの内容をフォーマットしますか?

また

  1. セルの内容を連結してSQLを準備しますか?

これを行う最良の方法は何ですか??

4

1 に答える 1

12

私は数週間前に同じことをするつもりでしたが、あなたの質問のExcel部分に対して次の解決策になりました。このソリューションは、新しいシート フォーマットと 97-2007 シート フォーマットの両方をサポートします。スプリングとPOIを使用しています。これ以上の情報がなければ、残りの質問に答えることはできないと思います。

ユーザーがファイルをアップロードする jsp サイト:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>import</title>
</head>
<body>
    <form:form modelAttribute="fileBean" method="post" enctype="multipart/form-data">
        <form:label for="fileData" path="fileData">Select file</form:label><br/><br/>
        <form:input path="fileData" type="file"/>
        <input type="submit" />
    </form:form>
</body>
</html>

送信時にトリガーされるコントローラー

@Controller
@RequestMapping("/upload")
public class ExcelImporterController {

    @RequestMapping(method = RequestMethod.POST)
    public String upload(FileBean uploadItem, BindingResult result) {
        importService.import(uploadItem);

        return "import/importDone";
    }

}

インターフェース..

public interface importService {

    public void import(FileBean fileBean);
}

import メソッドを使用したインターフェースの実装..

@Override
    public void import(FileBean fileBean) {

        ByteArrayInputStream bis = new ByteArrayInputStream(filedBean.getFileData().getBytes());
        Workbook workbook;
        try {
            if (fileBean.getFileData().getOriginalFilename().endsWith("xls")) {
                workbook = new HSSFWorkbook(bis);
            } else if (fileBean.getFileData().getOriginalFilename().endsWith("xlsx")) {
                workbook = new XSSFWorkbook(bis);
            } else {
                throw new IllegalArgumentException("Received file does not have a standard excel extension.");
            }

            for (Row row : sheet) {
               if (row.getRowNum() == 0) {
                  Iterator<Cell> cellIterator = row.cellIterator();
                  while (cellIterator.hasNext()) {
                      Cell cell = cellIterator.next();
                      //go from cell to cell and do create sql based on the content
                  }
               }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

FileBean での春のアップロードに使用される Bean の構成。

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10000000"/>
</bean>

ファイルビーン

public class FileBean {

  private CommonsMultipartFile fileData;

  public CommonsMultipartFile getFileData()
  {
    return fileData;
  }

  public void setFileData(CommonsMultipartFile fileData)
  {
    this.fileData = fileData;
  }

}

于 2013-06-25T11:39:52.837 に答える