根据excel文件生成表的索引

本文介绍了一个Java程序,该程序从指定目录下的Excel文件中读取数据,并为每个Excel工作表生成对应的SQL创建索引语句。这些语句被写入一个名为createIndex.sql的文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class createIndex {
 public static void main(String[] args) {
  try {
   FileWriter out = new FileWriter("C://createIndex.sql");
   File file = new File("f://KYOGOKU");
   String files[];
   files = file.list();
   for (int fileNo = 0; fileNo < files.length; fileNo++) {
    if(!files[fileNo].endsWith(".xls")){
     continue;
    }
    File f = new File("f://KYOGOKU//" + files[fileNo]);
    Workbook wb;
    wb = Workbook.getWorkbook(f);
    Sheet[] sheets = wb.getSheets();
    int indexColumnCount;
    for(int i=0; i <sheets.length;i++){
     String tableName = sheets[i].getCell(0, 3).getContents();
     int indexNo = 1;
     for(int col=8;col<sheets[i].getColumns();col++){
      indexColumnCount = 0;
      String strIndex = "create index "+tableName+"_INDEX"+indexNo+" on "+tableName+" (";
      for(int row=8;row<sheets[i].getRows();row++){
       if(sheets[i].getCell(col, row).getContents()!=""){
        strIndex += sheets[i].getCell(2, row).getContents();
        strIndex += ", ";
        indexColumnCount++;
       }
      }
      if(indexColumnCount == 0){
       strIndex = "";
      }else{
       strIndex = strIndex.substring(0, strIndex.length() - 2);
       strIndex += ");";
       strIndex += "/r/n";
       strIndex += "//";
       strIndex += "/r/n";
      }
      System.out.println(strIndex);
      if(strIndex!=""){
       out.write(strIndex);
      }
      indexNo++;
     } 
    }
   }
   out.close();
  } catch (FileNotFoundException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  } catch (BiffException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值