天天看點

基于jse的金蝶K3ERP-BOM物料編碼位置号比對工具(輸出excel)



 Long ago...之前接到硬體部的需求:要求做一個BOM比對工具,從K3的bom多級展開中拿到BOM的物料清單并把物料的的位置号取出并計數。

網上找了下K3的資料結構表,分析了下,用swing寫了個小程式,實作了需求。下面将代碼分享給各位看客。

技術關鍵詞:swing,JFrame,POI;

package controller;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


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;

class InputUIx extends JFrame {

 JTextField jTextField; // 定義文本框元件
 JLabel jLabel1;
 JPanel BOM_Name, jp2, jp3;
 JButton enter, cancel; // 建立按鈕

 public InputUIx() {

  // File file = chooser.getSelectedFile();

  jTextField = new JTextField(12);
  jLabel1 = new JLabel("BOM編碼:");
  enter = new JButton("确認");
  cancel = new JButton("退出");
  BOM_Name = new JPanel();
  jp3 = new JPanel();

  // 設定布局
  this.setLayout(new GridLayout(3, 1));

  BOM_Name.add(jLabel1);
  BOM_Name.add(jTextField);// 第一塊面闆添加jLabel1和文本框

  jp3.add(enter);
  jp3.add(cancel); // 第三塊面闆添加确認和取消

  String root = System.getProperty("user.dir");
  System.out.println(root);
  
  String strSql = "select A.FBOMNumber,B.FNote,B.FPositionNo,C.FNumber,B.FAuxQty "
    + "from ICBOM as A "
    + "left join ICBOMCHILD as B on A.FInterID = B.FInterID "
    + "left join t_Item as C on B.FItemID = C.FItemID "
    + "where A.FBOMNumber ='" + "BOMNumber" + "'";
  

  enter.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    String BOMNumber = jTextField.getText();
    if (jTextField.getText().equals("")) {
     JOptionPane.showMessageDialog(null, "錯誤:0001:BOM編号不能為空", "提示資訊",
       JOptionPane.ERROR_MESSAGE);
    } else {

     int autogrow = 1;

     try {
//      String BOMNumber = jTextField.getText();
      
      System.out.println("資料庫已連接配接! "); // 測試資料庫連接配接


      System.out.println(strSql);

      // 建立EXCEL,以poi方式輸出xlsx格式的excel
      XSSFWorkbook book = new XSSFWorkbook();
      XSSFSheet sheet = book.createSheet("BOM位置号統計表");

      XSSFRow row = sheet.createRow(0);

      XSSFCell cell = row.createCell(0);
      cell.setCellValue("BOM編号");
      cell = row.createCell(1);
      cell.setCellValue("物料編碼");
      cell = row.createCell(2);
      cell.setCellValue("位置号合計");
      cell = row.createCell(3);
      cell.setCellValue("計數");
      cell = row.createCell(4);
      cell.setCellValue("原計數");
      cell = row.createCell(5);
      cell.setCellValue("對比");

      System.out.println("5.循環次數:" + autogrow);
      System.out.println("---------------------");
      //生成含BOM編号的excel檔案
      String path = root + "\\" + BOMNumber + ".xlsx";

      FileOutputStream os = new FileOutputStream(path);
      System.out.println(path);
      book.write(os);

      os.close();
      // 結果集為空報錯
      JOptionPane.showMessageDialog(null, "文檔已生成!");
     }

     catch (Exception f) {

      f.printStackTrace();
      JOptionPane.showMessageDialog(null, "錯誤:0002:程式異常", "提示資訊",
        JOptionPane.ERROR_MESSAGE);
     }

    }

   }
  });

  // cancel 關閉程式
  cancel.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    dispose();
   }
  });

  this.add(BOM_Name);
  // this.add(jp2);
  this.add(jp3); // 将三塊面闆添加到登陸框上面
  // 設定顯示
  this.setSize(400, 150);
  // this.pack();
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.setVisible(true);
  this.setTitle("BOM位置号統計工具");
  this.setLocationRelativeTo(getOwner());

 }

}