Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 解锁从javaFX桌面应用导出的excel文件_File_Javafx_Export - Fatal编程技术网

File 解锁从javaFX桌面应用导出的excel文件

File 解锁从javaFX桌面应用导出的excel文件,file,javafx,export,File,Javafx,Export,我看着 我知道这篇文章很旧,我对excel文件也有同样的问题。我的场景是:我有一个用javaFX编写的桌面应用程序,用户将TableView内容导出到excel文件中。一切正常,除了文件试图打开时,用户锁定文件的消息进行编辑。 我如何强制或绕过从生成文件的应用程序解锁该文件? 这是我最初的代码 String directory = CreateDir(); if (rs != null) { File file_excel = new File(direct

我看着 我知道这篇文章很旧,我对excel文件也有同样的问题。我的场景是:我有一个用javaFX编写的桌面应用程序,用户将TableView内容导出到excel文件中。一切正常,除了文件试图打开时,用户锁定文件的消息进行编辑。 我如何强制或绕过从生成文件的应用程序解锁该文件? 这是我最初的代码

String directory = CreateDir();
        if (rs != null) {
           File file_excel = new File(directory + "\\" + reportName + ".xls");
                file_excel.createNewFile();
                while (rs.next()) {
                    pc.exportToXls_File(sql, uID, uPW, file_excel);
                }
                Desktop.getDesktop().open(file_excel);
            }
exportToXls\u文件函数

public void exportToXls_File(String sql, String userName, String passWord, File fileName)
            throws SQLException, FileNotFoundException, IOException, Exception {
        ActionEvent event = new ActionEvent();
        DBConnection dbc = new DBConnection();
        conn = dbc.getConnection(event, userName, passWord);
        try {
            try (
                    /**
                     * Create new Excel workbook and sheet
                     */
                    HSSFWorkbook xlsWorkbook = new HSSFWorkbook()) {

                HSSFSheet xlsSheet = xlsWorkbook.createSheet();
                short rowIndex = 0;
                /**
                 * Execute SQL query
                 */
                stmt = conn.prepareStatement(sql);
                rs = stmt.executeQuery();
                /**
                 * Get the list of column names and store them as the first Row
                 * of the spreadsheet.
                 */
                ResultSetMetaData colInfo = rs.getMetaData();
                List<String> colNames = new ArrayList<>();
                HSSFRow titleRow = xlsSheet.createRow(rowIndex++);

                for (int i = 1; i <= colInfo.getColumnCount(); i++) {
                    colNames.add(colInfo.getColumnName(i));
                    titleRow.createCell((int) (i - 1)).setCellValue(
                            new HSSFRichTextString(colInfo.getColumnName(i)));
                    xlsSheet.setColumnWidth((int) (i - 1), (short) 4000);
                }
                /**
                 * Save all the data from the database table rows
                 */
                while (rs.next()) {
                    HSSFRow dataRow = xlsSheet.createRow(rowIndex++);
                    int colIndex = 0;
                    for (String colName : colNames) {
                        dataRow.createCell(colIndex++).setCellValue(
                                new HSSFRichTextString(rs.getString(colName)));
                    }
                }
                /**
                 * Write to disk
                 */
                xlsWorkbook.write(new FileOutputStream(fileName));
                xlsWorkbook.close();
            }
        } catch (IOException | SQLException ex) {
            out.println(ex);
        } finally {

            if (conn != null) {
                stmt.close();
                closeConnection((OracleConnection) conn);
                conn.close();
            }
        }
    }
public void exportToXls_文件(字符串sql、字符串用户名、字符串密码、文件名)
抛出SQLException、FileNotFoundException、IOException、Exception{
ActionEvent=新的ActionEvent();
DBConnection dbc=新的DBConnection();
conn=dbc.getConnection(事件、用户名、密码);
试一试{
试一试(
/**
*创建新的Excel工作簿和工作表
*/
HSSFWorkbook xlsWorkbook=新的HSSFWorkbook()){
HSSFSheet xlsSheet=xlsWorkbook.createSheet();
短行索引=0;
/**
*执行SQL查询
*/
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery();
/**
*获取列名列表并将其存储为第一行
*电子表格的一部分。
*/
ResultSetMetaData colInfo=rs.getMetaData();
List colNames=new ArrayList();
HSSFRow titleRow=xlsSheet.createRow(rowIndex++);

对于(int i=1;i,您得到的问题是什么?问题是如何表现出来的?您写入文件的方式似乎是一种)非常低效,因为您正在为每个db行或b)读取和写入文件,您从未关闭文件,这意味着您正在自己创建问题。通常代码如下:
Document doc=createNewInMemoryDoc();而(rs.next()){addRowToDoc(doc,rs);}saveToFile(doc,file\u excel);桌面…
通常不需要
createNewFile
。谢谢fabian的快速回答。我包括了exportToXls\u File函数,可以提供整个图片。我没有理解你所说的
createNewInMemoryDoc()
addRowToDoc(doc,rs)
saveToFile(doc,File\excel)是什么意思
;您正在为某些
结果集中的每一行导出
,但是在第二次查看时,您似乎没有使用将任何信息从结果集中传递到
exportToXls\u文件
方法。我只是想演示通常的方法,即:1.初始化文档数据结构2.使用g a循环3.将数据写入输出文件…正在创建文件并将数据写入其中。我让应用程序锁定该文件并阻止其打开的问题。