隨筆-11  評論-5  文章-1  trackbacks-0
          import ?java.io. * ;
          import ?java.util.prefs. * ;
          import ?java.sql. * ;
          import ?jxl. * ;
          import ?jxl.write. * ;
          import ?java.util. * ;
          import ?java.util.Properties;
          import ?com.lowagie.text. * ;
          import ?com.lowagie.text.pdf. * ;
          import ?java.awt.Color;

          /*
          ?*author:kenshin
          ?*effect:from?database?reading?values?,output?regedit?or?text?or?xml?or?excel?or?pdf.
          ?*create?time:2004/6/8
          ?*modify?time:2004/6/9
          ?*copyright:2004
          */

          public ? class ?PrefsDemo? {

          ??
          static ?Preferences?prefsdemo;
          ??
          static ?String[]?field = new ??String[ 1000 ];
          ??
          static ?String?[]?values = new ?String[ 1000 ];
          ??
          static ? int ?account = 0 ,maxCount = 0 ;
          ??
          static ?String?path = null ,driver = null ,user = null ,password = null ,odbcname = null ;
          ??
          static ?String?txtname = null ,xmlname = null ,excelname = null ,pdfname = null ,sql = null ;
          ??
          static ?String?author = null ,createtime = null ,effect = null ,version = null ;

          ??
          public ? static ? void ?main(String?args[]) {
          ???String?rnumber
          = null ;
          ???
          int ?number = 0 ;
          ???BufferedReader?buf;

          ????
          // initialize?config?file
          ????Initconfig();
          ???
          // connection?database
          ???DBconnection();
          ???
          // goto?enterLoop
          ???enterLoop:
          ????
          while (number <= 6 ) {
          ????
          try {
          ?????
          // choice?output?formatting
          ?????System.out.println( " please?choice?input?formatting!!! " );
          ?????System.out.print(
          " 1:input?regedit???2:input?xml???3:input?text " );
          ?????System.out.println(
          " ???4:excel???5:pdf???6:all???7:exit " );
          ?????System.out.print(
          " input?a?number,not?a?character: " );
          ??????
          // keyword?input
          ?????buf = new ?BufferedReader( new ?InputStreamReader(System.in));
          ?????rnumber
          = buf.readLine();
          ?????
          // change?"number"?type
          ?????number = Integer.parseInt(rnumber);
          ?????
          if (number > 7 ) {
          ??????System.out.println(
          " please?input?an?number?of?between?1?to?7? " );
          ??????number
          = 0 ;
          ??????
          continue ?enterLoop;
          ?????}

          ?????
          switch (number) {
          ??????
          case ? 1 :
          ??????????????????????
          // output?regedit
          ??????????????????????RegeditOutput();? break ;
          ????????????????
          case ? 2 :
          ??????????????????????
          // output?xml
          ??????????????????????XmlOutput();????? break ;
          ????????????????
          case ? 3 :
          ??????????????????????
          // output?text
          ??????????????????????TextOutput();???? break ;
          ????????????????
          case ? 4 :
          ??????????????????????
          // output?excel
          ??????????????????????ExcelOutput();??? break ;
          ????????????????
          case ? 5 :
          ??????????????????????
          // output?excel
          ??????????????????????PdfOutput();??? break ;
          ????????????????
          case ? 6 :
          ??????????????????????
          // output?all
          ??????????????????????RegeditOutput();
          ??????????????????????XmlOutput();
          ??????????????????????TextOutput();
          ??????????????????????ExcelOutput();
          ??????????????????????PdfOutput();
          ??????????????????????
          break ;
          ?????}

          ????}

          ????
          catch (IOException?e) {System.err.println(e);}
          ????
          catch (NumberFormatException?e) {
          ?????System.out.println(
          " please?input?an?number?type! " );
          ?????number
          = 0 ;
          ?????
          continue ?enterLoop;
          ????}

          ????}

          ??}

          ??
          /** ***********************************************
          ???*effect:initialize?config?file
          ???*input?value?:null
          ???*return?value:null
          ???*create?time:2004/6/11
          ???*edit?time:null
          ???************************************************
          */

          ??
          static ? void ?Initconfig() {

          ???Properties?prop?
          = ? new ?Properties();
          ???String?propFileName?
          = ? " config.properties " ;
          ???FileInputStream?fis;

          ???
          try {
          ????fis?
          = ? new ?FileInputStream( new ?File(propFileName));
          ??prop.load(fis);
          ??
          // get?config?file's?values
          ?????path? = ?prop.getProperty( " path " );
          ?????odbcname?
          = ?prop.getProperty( " odbc " );
          ?????driver?
          = ?prop.getProperty( " driver " );
          ?????user?
          = ?prop.getProperty( " user " );
          ?????password?
          = ?prop.getProperty( " password " );

          ?????txtname?
          = ?prop.getProperty( " txtname " );
          ?????xmlname?
          = ?prop.getProperty( " xmlname " );
          ?????excelname?
          = ?prop.getProperty( " excelname " );
          ?????pdfname?
          = ?prop.getProperty( " pdfname " );

          ?????author?
          = ?prop.getProperty( " author " );
          ?????createtime?
          = ?prop.getProperty( " createtime " );
          ?????effect?
          = ?prop.getProperty( " effect " );
          ?????version?
          = ?prop.getProperty( " version " );

          ?????sql?
          = ?prop.getProperty( " sql " );

          ?????
          // close?inputstream
          ??fis.close();

          ????System.out.println(
          " ************************* " );
          ????System.out.println(
          " *config?read?successfull* " );
          ????System.out.println(
          " ************************* " );
          ???}

          ???
          catch (IOException?e) {System.err.println(e);}
          ??}

          ??
          /** ***********************************************
          ???*effect:connection?database
          ???*input?value?:null
          ???*return?value:null
          ???*create?time:2004/6/9
          ???*edit?time:null
          ???************************************************
          */

          ??
          static ? void ?DBconnection() {
          ???
          // jdbc-odbc?variable?initialize
          ???Connection?con = null ;
          ???Statement?stm
          = null ;
          ???ResultSet?rs
          = null ;

          ???
          // jdbc-odbc?bridge
          ??? try {
          ????Class.forName(driver);
          ????con
          = DriverManager.getConnection(odbcname,user,password);
          ????stm
          = con.createStatement();
          ????rs
          = stm.executeQuery(sql);

          ????
          // get?columncount?values
          ????????account = rs.getMetaData().getColumnCount();

          ????System.out.println(
          " ************************* " );
          ????System.out.println(
          " *DB?connect?successfull?* " );
          ????System.out.println(
          " ************************* " );
          ????????
          // get?database?ColumnName
          ???????? for ( int ?i = 1 ;i <= account;i ++ ) {
          ????????????field[i]
          = rs.getMetaData().getColumnName(i);
          ????????}

          ????
          while (rs.next()) {
          ?????????????
          for ( int ?j = 1 ;j <= account;j ++ ) {
          ???????????????
          ++ maxCount;
          ?????????????????values[maxCount]
          = rs.getString(field[j]);
          ?????????????????System.out.println(values[maxCount]);
          ?????????????}

          ????}

          ????
          // close?database
          ????rs.close();
          ????stm.close();
          ????con.close();
          ???}

          ????
          catch (Exception?e) {
          ?????System.err.println(e);
          ????}

          ??}

          ??
          /** ***********************************************
          ???*effect:output?regedit
          ???*input?value?:null
          ???*return?value:null
          ???*create?time:2004/6/9
          ???*edit?time:null
          ???************************************************
          */

          ??
          static ? void ?RegeditOutput() {
          ?prefsdemo?
          = ?Preferences.userRoot().node( " /com/sunway/spc " );

          ?
          // wirte?regedit
          ?prefsdemo.put( " author " ,author);
          ?prefsdemo.put(
          " createtime " ,createtime);
          ?prefsdemo.put(
          " effect " ,effect);
          ?prefsdemo.put(
          " version " ,version);

          ???System.out.println(
          " regedit?create?successfull! " );
          ??}

          ??
          /** ***********************************************
          ???*effect:output?xml
          ???*input?value?:null
          ???*return?value:null
          ???*create?time:2004/6/9
          ???*edit?time:null
          ???************************************************
          */

          ??
          static ? void ?XmlOutput() {
          ????
          // write?xml
          ???? try {
          ??File?myfile?
          = ? new ?File(path + xmlname);
          ??
          if ( ! (myfile.exists())) {
          ???myfile.createNewFile();
          ??}

          ??
          // ?write?file
          ?????FileOutputStream?fos? = ? new ?FileOutputStream(path + xmlname);
          ?????prefsdemo.exportNode(fos);
          ????}

          ????
          catch ?(Exception?e) {
          ???????System.err.println(
          " Cannot?export?nodes:? " ? + ?e);
          ????}

          ???System.out.println(
          " xml?create?successfull? " );
          ??}

          ??
          /** ***********************************************
          ???*effect:output?text
          ???*input?value?:null
          ???*return?value:null
          ???*create?time:2004/6/9
          ???*edit?time:null
          ???************************************************
          */

          ??
          static ? void ?TextOutput() {

          ?
          // field?length
          ? int ?length1[] = new ? int [ 1000 ];
          ?
          // values?length
          ? int ?length2[] = new ? int [ 1000 ];
          ?String?space
          = " ?????????? " ;
          ?String?str1
          = " ------------------------- " ;
          ?String?str2[]
          = new ?String[ 1000 ];
          ?String?str3[]
          = new ?String[ 1000 ];
          ?
          // check?"/n"
          ? int ?q = 1 ;
          ??
          // initialize?str1[]?str2[]
          ? for ( int ?i = 1 ;i <= maxCount;i ++ ) {
          ??str2[i]
          = "" ;
          ??str3[i]
          = "" ;
          ?}


          ?
          try {
          ?????
          // create?file
          ??File?myfile? = ? new ?File(path + txtname);
          ??
          if ( ! (myfile.exists())) {
          ???myfile.createNewFile();
          ??}

          ??
          // ?write?file
          ??FileWriter?fw = new ?FileWriter(path + txtname);

          ??
          for ( int ?i = 1 ;i <= maxCount;i ++ ) {
          ???
          if (i <= account) {
          ????length1[i]
          = 25 - field[i].length();
          ????
          // account?length2?length
          ???? for ( int ?j = 1 ;j <= length1[i];j ++ ) {
          ?????str2[i]
          = str2[i] + " ? " ;
          ????}

          ???}

          ???length2[i]
          = 25 - values[i].length();
          ???
          // account?length3?length
          ??? for ( int ?j = 1 ;j <= length2[i];j ++ ) {
          ????str3[i]
          = str3[i] + " ? " ;
          ???}

          ??}

          ??
          // write?field
          ?? for ( int ?i = 1 ;i <= account;i ++ ) {
          ???fw.write(field[i]);
          ???fw.write(str2[i]
          + space);
          ??}

          ??
          // write?"-"
          ??fw.write( " \n " );
          ??
          for ( int ?i = 1 ;i <= account;i ++ ) {
          ???fw.write(str1
          + space);
          ??}

          ??
          // write?values
          ??fw.write( " \n " );
          ??
          for ( int ?i = 1 ;i <= maxCount;i ++ ) {
          ???fw.write(values[i]
          + str3[i] + space);
          ???
          if (i == account * q) {
          ????fw.write(
          " \n " );
          ????q
          *= 2 ;
          ???}

          ??}

          ??fw.close();
          ?}

          ?
          catch (FileNotFoundException?e) {System.err.println(e);}
          ?
          catch (IOException?e) {e.printStackTrace();}
          ???System.out.println(
          " text?create?successfull? " );
          ??}

          ??
          /** ***********************************************
          ???*effect:output?excel
          ???*input?value?:null
          ???*return?value:null
          ???*create?time:2004/6/9
          ???*edit?time:2004/6/10
          ???************************************************
          */

          ??
          static ? void ?ExcelOutput() {

          ?
          // label1:field????label2:values
          ??Label?label1 = null ,label2 = null ;
          ?
          int ?result = 0 ,rows = 0 ;
          ?
          try {
          ??
          // create?an?new?file
          ??File?myfile? = ? new ?File(path + excelname);
          ??
          if ( ! (myfile.exists())) {
          ???myfile.createNewFile();
          ???System.out.println(
          " an?new?excel?create?successful " );
          ??}

          ?????
          // create?an?new?excel
          ?????WritableWorkbook?workbook? = ?Workbook.createWorkbook(myfile);
          ?????
          // use?first?excel's?sheet
          ?????WritableSheet?sheet? = ?workbook.createSheet( " record " ,? 0 );

          ?????
          // ?add?table?title?and?values
          ????? for ( int ?i = 1 ;i <= account;i ++ ) {
          ?????????
          // add?table?title
          ?????????label1? = ? new ?Label(i - 1 ,? 0 ,?field[i]);
          ?????????sheet.addCell(label1);
          ?????}

          ?????
          // account?rows?values
          ?????result = maxCount / account;
          ?????
          for ( int ?i = 1 ;i <= result;i ++ ) {
          ??????
          for ( int ?m = 1 ;m <= account;m ++ ) {
          ???????rows
          ++ ;
          ??????????label2
          = new ?Label(m - 1 ,i,values[rows]);
          ??????????sheet.addCell(label2);
          ??????}

          ?????}


          ?????workbook.write();
          ?????workbook.close();
          ?}

          ?
          catch (Exception?e) {System.err.println(e);}
          ?System.out.println(
          " excel?create?successfull? " );
          ??}

          ??
          /** ***********************************************
          ???*effect:output?pdf?file
          ???*input?value?:null
          ???*return?value:null
          ???*create?time:2004/6/11
          ???*edit?time:null
          ???************************************************
          */

          ??
          static ? void ?PdfOutput() {
          ????Document?document;
          ????Rectangle?pageRect;

          ???
          try {
          ??document
          = new ?Document(PageSize.A4,? 50 ,? 50 ,? 100 ,? 50 );
          ??pageRect
          = document.getPageSize();
          ??PdfWriter.getInstance(document,?
          new ?FileOutputStream(path + pdfname));

          ??HeaderFooter?header?
          = ? new ?HeaderFooter( new ?Phrase( " student?information " ),? false );
          ??header.setBorder(
          2 );
          ??header.setAlignment(Element.ALIGN_RIGHT);
          ??document.setHeader(header);

          ????????
          // add?page
          ??HeaderFooter?footer? = ? new ?HeaderFooter( new ?Phrase( " the " ), new ?Phrase( " page " ));
          ??footer.setAlignment(Element.ALIGN_CENTER);
          ??footer.setBorder(
          1 );
          ??document.setFooter(footer);

          ??
          // open?document
          ??document.open();

          ????????
          // create?table
          ??Table?table? = ? new ?Table(account);
          ??table.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE);
          ??table.setBorder(Rectangle.NO_BORDER);

          ??
          int ?hws[]? = ? new ? int [account];
          ??
          for ( int ?i = 1 ;i <= account;i ++ ) {
          ???hws[i
          - 1 ] = 20 ;
          ??}


          ??table.setWidths(hws);
          ??table.setWidth(
          100 );
          ??
          // add?field
          ?? for ( int ?i = 1 ;i <= account;i ++ ) {
          ???table.addCell(
          new ?Phrase(field[i]));
          ??}

          ??
          // add?values
          ?? for ( int ?i = 1 ;i <= maxCount;i ++ ) {
          ???table.addCell(
          new ?Phrase(values[i]));
          ??}


          ??
          // add?table?to?pdf?file
          ??document.add(table);
          ??
          // close?pdf?file
          ??document.close();
          ????}

          ???
          catch (Exception?e) {System.out.println(e);}
          ?System.out.println(
          " pdf?create?successfull? " );
          ??}

          }

          posted on 2006-10-24 20:12 Crespo 閱讀(2169) 評論(1)  編輯  收藏

          評論:
          # re: java讀取數(shù)據(jù)庫字段和值,輸出到regedit,xml,text,excel,pdf的程序 2006-10-25 08:58 | illusion
          利用Java 創(chuàng)建和讀取Excel文檔
          import org.apache.poi.hssf.usermodel.HSSFWorkbook;
          import org.apache.poi.hssf.usermodel.HSSFSheet;
          import org.apache.poi.hssf.usermodel.HSSFRow;
          import org.apache.poi.hssf.usermodel.HSSFCell;
          import java.io.FileOutputStream;
          public class CreateXL {
           /** Excel 文件要存放的位置,假定在D盤JTest目錄下*/
           public static String outputFile="D:/JTest/ gongye.xls";
           public static void main(String argv[])
          {
           try
          {
            // 創(chuàng)建新的Excel 工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            // 在Excel工作簿中建一工作表,其名為缺省值
            // 如要新建一名為"效益指標"的工作表,其語句為:
            // HSSFSheet sheet = workbook.createSheet("效益指標");
            HSSFSheet sheet = workbook.createSheet();
            // 在索引0的位置創(chuàng)建行(最頂端的行)
            HSSFRow row = sheet.createRow((short)0);
            //在索引0的位置創(chuàng)建單元格(左上端)
            HSSFCell cell = row.createCell((short) 0);
            // 定義單元格為字符串類型
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
            // 在單元格中輸入一些內容
            cell.setCellValue("增加值");
            // 新建一輸出文件流
            FileOutputStream fOut = new FileOutputStream(outputFile);
            // 把相應的Excel 工作簿存盤
            workbook.write(fOut);
            fOut.flush();
            // 操作結束,關閉文件
            fOut.close();
            System.out.println("文件生成...");



           }catch(Exception e) {
            System.out.println("已運行 xlCreate() : " + e );
           }
          }
          }



          讀取Excel文檔中的數(shù)據(jù)
            示例2將演示如何讀取Excel文檔中的數(shù)據(jù)。假定在D盤JTest目錄下有一個文件名為gongye.xls的Excel文件。
            示例2程序如下:
          import org.apache.poi.hssf.usermodel.HSSFWorkbook;
          import org.apache.poi.hssf.usermodel.HSSFSheet;
          import org.apache.poi.hssf.usermodel.HSSFRow;
          import org.apache.poi.hssf.usermodel.HSSFCell;
          import java.io.FileInputStream;
          public class ReadXL {
           /** Excel文件的存放位置。注意是正斜線*/
           public static String fileToBeRead="D:/JTest/ gongye.xls";
           public static void main(String argv[]){
           try{
            // 創(chuàng)建對Excel工作簿文件的引用
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
            // 創(chuàng)建對工作表的引用。
            // 本例是按名引用(讓我們假定那張表有著缺省名"Sheet1")
            HSSFSheet sheet = workbook.getSheet("Sheet1");
            // 也可用getSheetAt(int index)按索引引用,
            // 在Excel文檔中,第一張工作表的缺省索引是0,
            // 其語句為:HSSFSheet sheet = workbook.getSheetAt(0);
            // 讀取左上端單元
            HSSFRow row = sheet.getRow(0);
            HSSFCell cell = row.getCell((short)0);
            // 輸出單元內容,cell.getStringCellValue()就是取所在單元的值
            System.out.println("左上端單元是: " + cell.getStringCellValue());
           }catch(Exception e) {
            System.out.println("已運行xlRead() : " + e );
           }
          }
          }




            設置單元格格式
            在這里,我們將只介紹一些和格式設置有關的語句,我們假定workbook就是對一個工作簿的引用。在Java



          中,第一步要做的就是創(chuàng)建和設置字體和單元格的格式,然后再應用這些格式:



            1、創(chuàng)建字體,設置其為紅色、粗體:
          HSSFFont font = workbook.createFont();
          font.setColor(HSSFFont.COLOR_RED);
          font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            2、創(chuàng)建格式
          HSSFCellStyle cellStyle= workbook.createCellStyle();
          cellStyle.setFont(font);
            3、應用格式
          HSSFCell cell = row.createCell((short) 0);
          cell.setCellStyle(cellStyle);
          cell.setCellType(HSSFCell.CELL_TYPE_STRING);
          cell.setCellValue("標題 ");

            回復  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 航空| 安宁市| 灌阳县| 太谷县| 清涧县| 杨浦区| 剑阁县| 武定县| 和静县| 读书| 姚安县| 东莞市| 凭祥市| 怀宁县| 开鲁县| 泽库县| 海阳市| 布拖县| 松溪县| 平凉市| 金门县| 承德县| 稻城县| 鹰潭市| 呼图壁县| 渝北区| 阳原县| 郯城县| 五华县| 尚义县| 建昌县| 灵璧县| 天长市| 辽中县| 武邑县| 门头沟区| 花莲市| 西乌珠穆沁旗| 浠水县| 东阳市| 木里|