Quantcast
Channel: BTSoru.com - Bilisim ve Yazilim Teknolojileri Soru/Cevap Platformu - latest questions
Viewing all articles
Browse latest Browse all 4270

Apache poi ile excel(.xlsx) okuma-yazma

$
0
0

Merhaba arkadşalar Apache poi ile Excel dosyası okuyup yazabiliyorum ancak 2003 ve altı versiyonları(.xls). 2007 ve üstü(.xlsx) için HSSF yerine XSSF i kullanın deniliyor ancak XSSF kullandığımda da 2007 ve üstü için işlem yapamıyorum.Acaba ekstradan bir kod mu yazmam gerekiyor yoksa bir hatam mı var?

Ben sizlere üzerinde çalıştığım Class'ın tamamını yazıyorum arkadaşlar umarım ki sorunu çözebiliriz :)

 package control;
 import java.awt.Dimension;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.util.Vector;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.swing.JFileChooser;
 import javax.swing.JFrame;
 import javax.swing.JOptionPane;
 import javax.swing.table.DefaultTableModel;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFRow;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import view.ExcelIslem_JFrame;

  public class ExcelIslem_Kontrol {
    ExcelIslem_JFrame excelIslem_JFrame;
    static JFileChooser jChooser=new JFileChooser(); 
    static int tableWidth = 0;
    static int tableHeight = 0;

    static Vector headers = new Vector();
    static Vector data = new Vector();
        DefaultTableModel model = null;

            public static void main(String[] args) {
    ExcelIslem_Kontrol excelIslem_Kontrol = new ExcelIslem_Kontrol();
        }

       public ExcelIslem_Kontrol() {
    excelIslem_JFrame = new ExcelIslem_JFrame();
    excelIslem_JFrame.setVisible(true);
    actionListenerYukle();  
         }
private void actionListenerYukle() {
    excelIslem_JFrame.btnCikis.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
         excelIslem_JFrame.setVisible(false);       
  }
    });

    excelIslem_JFrame.btnSec.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {

    jChooser.showOpenDialog(null); 
            jChooser.setDialogTitle("Okunacak Olan Excel Dosya Seçimi");
            File file = jChooser.getSelectedFile();

           if(file==null)
             {
                 JOptionPane.showMessageDialog(null, "Dosya Seçimi yapılmadı","Uyarı",     JOptionPane.INFORMATION_MESSAGE); 
                 return;
               }
           else if(!file.getName().endsWith("xls"))
              { 
             JOptionPane.showMessageDialog(null, "Excel Dosyanız 2003 ve altı değil", "Hata",JOptionPane.ERROR_MESSAGE); 
              }
           else 
             { 
               excelIslem_JFrame.textField.setText(jChooser.getSelectedFile().getAbsolutePath());
                   fillData(file);
                   model = new DefaultTableModel(data, headers); 
                   tableWidth = model.getColumnCount() * 150; 
                   tableHeight = model.getRowCount() * 25; 
                   excelIslem_JFrame.tableExcelIslemleri.setPreferredSize(new Dimension( tableWidth, tableHeight));
                   excelIslem_JFrame.tableExcelIslemleri.setModel(model); 
                }
        }

        @SuppressWarnings("unchecked")
        private void fillData(File file) {
             int index=-1;
             XSSFWorkbook workbook = null; 
            try { 
                   try { 
                           FileInputStream inputStream = new FileInputStream (file);
                            workbook = new XSSFWorkbook(inputStream);
                        } 
                   catch (IOException ex) 
                        { 
              Logger.getLogger(ExcelIslem_JFrame.class. getName()).log(Level.SEVERE, null, ex);
                         }

                           String[] strs=new String[workbook.getNumberOfSheets()];
                            for (int i = 0; i < strs.length; i++) {
                                 strs[i]= workbook.getSheetName(i); }
                            JFrame frame = new JFrame("Input Dialog");

                            String selectedsheet = (String) JOptionPane.showInputDialog(
                               frame, "Hangi sayfayı okumak istersiniz?", "Sayfa Seçimi",
                              JOptionPane.QUESTION_MESSAGE, null, strs, strs[0]);

                           if (selectedsheet!=null) {
                                for (int i = 0; i < strs.length; i++)
                                  {
               if (workbook.getSheetName(i).equalsIgnoreCase(selectedsheet))                                                                                                         
                              index=i; 
                                       }
                                XSSFSheet sheet = workbook.getSheetAt(index);
                                XSSFRow row=sheet.getRow(0);

                               headers.clear();
                               for (int i = 0; i < row.getLastCellNum(); i++)
                              {
                                 XSSFCell cell1 = row.getCell(i);
                                 headers.add(cell1.toString());
                              }

                              data.clear();
                              for (int j = 1; j < sheet.getLastRowNum() + 1; j++)
                              {
                                 @SuppressWarnings("rawtypes")
                     Vector d = new Vector();
                                 row=sheet.getRow(j);
                                 int noofrows=row.getLastCellNum();
                                 for (int i = 0; i < noofrows; i++)
                                 {    //To handle empty excel cells 
                                       XSSFCell cell=row.getCell(i,org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK );
                                       d.add(cell.toString());
                                 }
                                d.add("\\n");
                                data.add(d);
                              }
                         }
                        else { return; }
            }
          catch (Exception e) { e.printStackTrace(); }              
        }
    });
}}

Viewing all articles
Browse latest Browse all 4270

Trending Articles


Mide ağrısı için


Alessandra Torre - Karanlık Yalanlar


Şekilli süslü hazır floodlar


Flatcast Güneş ve Ay Flood Şekilleri


Gone Are the Days (2018) (ENG) (1080p)


Yildiz yükseltme


yc82


!!!!!!!!!! Amın !!!!!!!!!


Celp At Nalı (Sahih Tılsım)


SCCM 2012 Client Installation issue