📄 queryeditorresultsexporter.java
字号:
if (MiscUtils.isNull(value)) { GUIUtilities.displayErrorMessage("You must specify a file to export to."); return; } // check if it exists if (FileUtils.fileExists(value)) { int confirm = GUIUtilities. displayConfirmCancelDialog("Overwrite existing file?"); if (confirm == JOptionPane.CANCEL_OPTION) { return; } else if (confirm == JOptionPane.NO_OPTION) { fileNameField.selectAll(); fileNameField.requestFocus(); return; } } if (getExportFormatType() == ImportExportProcess.DELIMITED && delimCombo.getSelectedIndex() == 4) { value = customDelimField.getText(); if (MiscUtils.isNull(value)) { GUIUtilities.displayErrorMessage( "You must enter a custom delimeter"); return; } } SwingWorker worker = new SwingWorker() { public Object construct() { return doExport(); } public void finished() { GUIUtilities.displayInformationMessage( "Result set export complete."); dispose(); } }; worker.start(); } private Object doExport() { if (getExportFormatType() == ImportExportProcess.DELIMITED) { return exportDelimited(); } else { return exportExcel(); } } private Object exportExcel() { HSSFWorkbook workbook = null; OutputStream fout = null; ResultsProgressDialog progressDialog = null; try { File exportFile = new File(fileNameField.getText()); fout = new FileOutputStream(exportFile, false); workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Result Set Export"); int rowCount = model.getRowCount(); int columnCount = model.getColumnCount(); progressDialog = new ResultsProgressDialog(rowCount); setVisible(false); progressDialog.pack(); progressDialog.setLocation(GUIUtilities.getLocationForDialog( progressDialog.getSize())); progressDialog.setVisible(true); HSSFRow row = null; HSSFCell cell = null; String EMPTY = ""; int currentRow = 0; if (columnHeadersCheck.isSelected()) { row = sheet.createRow(currentRow); HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle style = workbook.createCellStyle(); style.setFont(font); for (int i = 0; i < columnCount; i++) { cell = row.createCell((short)i); cell.setCellValue(model.getColumnName(i)); cell.setCellStyle(style); } style = null; font = null; currentRow++; } for (int i = 0; i < rowCount; i++) { row = sheet.createRow(currentRow); for (short j = (short)0; j < columnCount; j++) { Object value = model.getValueAt(i, j); cell = row.createCell(j); cell.setCellValue(value != null ? value.toString() : EMPTY); } currentRow++; progressDialog.increment(i+1); } workbook.write(fout); return "done"; } catch (Exception e) { String message = "Error writing to file:\n\n" + e.getMessage(); GUIUtilities.displayExceptionErrorDialog(message, e); return "failed"; } finally { if (progressDialog != null && progressDialog.isVisible()) { progressDialog.dispose(); progressDialog = null; } if (fout != null) { try { fout.close(); } catch (IOException e) {} } fout = null; workbook = null; } } private Object exportDelimited() { int delimIndex = delimCombo.getSelectedIndex(); char delim = 0; switch (delimIndex) { case 0: delim = '|'; break; case 1: delim = ','; break; case 2: delim = ';'; break; case 3: delim = '#'; break; case 4: delim = customDelimField.getText().charAt(0); break; } ResultsProgressDialog progressDialog = null; PrintWriter writer = null; File exportFile = null; try { exportFile = new File(fileNameField.getText()); String EMPTY = ""; StringBuffer rowLines = new StringBuffer(5000); writer = new PrintWriter(new FileWriter(exportFile, false), true); int rowCount = model.getRowCount(); int columnCount = model.getColumnCount(); progressDialog = new ResultsProgressDialog(rowCount); setVisible(false); progressDialog.pack(); progressDialog.setLocation(GUIUtilities.getLocationForDialog( progressDialog.getSize())); progressDialog.setVisible(true); if (columnHeadersCheck.isSelected()) { for (int i = 0; i < columnCount; i++) { rowLines.append(model.getColumnName(i)); if (i != columnCount - 1) { rowLines.append(delim); } } writer.println(rowLines.toString()); rowLines.setLength(0); } for (int i = 0; i < rowCount; i++) { for (int j = 0; j < columnCount; j++) { Object value = model.getValueAt(i, j); rowLines.append(value != null ? value : EMPTY); if (j != columnCount - 1) { rowLines.append(delim); } } writer.println(rowLines.toString()); rowLines.setLength(0); progressDialog.increment(i+1); } /* for (int i = 0; i < totalRows; i++) { ArrayList row = (ArrayList)model.getRowValueAt(i); for (int j = 0; j < columnCount; j++) { Object value = row.get(j); rowLines.append(value != null ? value : EMPTY); if (j != columnCount - 1) { rowLines.append(delim); } } writer.println(rowLines.toString()); rowLines.setLength(0); progressDialog.increment(i+1); } */ return "done"; } catch (Exception e) { String message = "Error writing to file:\n\n" + e.getMessage(); GUIUtilities.displayExceptionErrorDialog(message, e); return "failed"; } finally { if (progressDialog != null && progressDialog.isVisible()) { progressDialog.dispose(); progressDialog = null; } if (writer != null) { writer.close(); } } } class ResultsProgressDialog extends JDialog { // the progess bar private JProgressBar progressBar; public ResultsProgressDialog(int recordCount) { super(GUIUtilities.getParentFrame(), "Exporting Query Results", false); progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, recordCount); JPanel base = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.anchor = GridBagConstraints.NORTHWEST; gbc.fill = GridBagConstraints.HORIZONTAL; base.add(new JLabel("Exporting result set..."), gbc); gbc.gridy = 1; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.insets.top = 0; gbc.ipadx = 180; gbc.insets.bottom = 10; gbc.fill = GridBagConstraints.BOTH; base.add(progressBar, gbc); base.setBorder(BorderFactory.createEtchedBorder()); Container c = this.getContentPane(); c.setLayout(new GridBagLayout()); c.add(base, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); setResizable(false); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); } public void increment(int value) { progressBar.setValue(value); } public void dispose() { setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); try { Thread.sleep(500); } catch (InterruptedException e) {} setVisible(false); super.dispose(); } } // class ResultsProgressDialog }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -