Created
April 21, 2016 13:48
-
-
Save sangpt/e09d11c1c23cf5fe046aa8181a952abd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed | |
JFileChooser fc = new JFileChooser(); | |
fc.setAcceptAllFileFilterUsed(false); | |
fc.addChoosableFileFilter(new ImageFilter()); | |
fc.setBounds(10, 10, 500, 500); | |
int value = fc.showOpenDialog(null); | |
File file = fc.getSelectedFile(); | |
if(value == JFileChooser.OPEN_DIALOG){ | |
file = fc.getSelectedFile(); | |
} | |
if(value == JFileChooser.CANCEL_OPTION){ | |
//JOptionPane.showMessageDialog(null, "Chọn file excel cần nhập"); | |
return ; | |
} | |
try { | |
//File file = new File("E:\\demo.xls"); | |
Workbook wb = Workbook.getWorkbook(file); | |
Sheet sheet = wb.getSheet(0); | |
int rows = sheet.getRows(); | |
int cols = sheet.getColumns(); | |
for(int row1 = 2; row1 < rows; row1++) { | |
// for(int col = 0; col < cols; col++) { | |
// System.out.print(sheet.getCell(col, row).getContents()+" "); | |
try { | |
String sql = "insert into SV values (?,?,?,?,?,?)"; | |
KetNoi kn = new KetNoi(); | |
Connection con = kn.getConnectionToSQL(); | |
PreparedStatement pst = con.prepareStatement(sql); | |
pst.setString(1, sheet.getCell(0, row1).getContents()); | |
pst.setString(2, sheet.getCell(1, row1).getContents()); | |
pst.setString(3, sheet.getCell(2, row1).getContents()); | |
pst.setString(4, sheet.getCell(3, row1).getContents()); | |
pst.setString(5, sheet.getCell(4, row1).getContents()); | |
pst.setString(6, sheet.getCell(5, row1).getContents()); | |
pst.executeUpdate(); | |
} catch (SQLException ex) { | |
Logger.getLogger(CNSV.class.getName()).log(Level.SEVERE, null, ex); | |
JOptionPane.showMessageDialog(null, "Thêm lỗi!"); | |
} | |
// } | |
} | |
} catch (IOException | BiffException | IndexOutOfBoundsException ex) { | |
Logger.getLogger(CNSV.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
JOptionPane.showMessageDialog(null, "Nhập file excel thành công!"); | |
}//GEN-LAST:event_jButton6ActionPerformed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment