Luben Park

          Java Ben 成長之路

          java中利用POI處理Excel

          package txtToExcel;

          import org.apache.poi.hssf.usermodel.HSSFCellStyle;
          import org.apache.poi.hssf.usermodel.HSSFDataFormat;
          import org.apache.poi.hssf.usermodel.HSSFFont;
          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.BufferedReader;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.FileReader;
          import java.io.IOException;
          import java.io.InputStreamReader;
          import java.net.URL;
          import java.util.ArrayList;

          /**
           * @author lx
           *
           */
          public class TxtToExecl {
           /** Excel 文件要存放的位置,假定在C盤下 */
           public static String outputFile = "c:\\11.xls";
           
           private static String[] DataOfLine = null;

           private static ArrayList array = new ArrayList();

           public static void main(String argv[]) {
            try {
             String fromPath = "c:/11.txt";// 取得當前目錄的路徑
             FileReader txtReader = new FileReader(fromPath);// 建立FileReader對象,并實例化為txtReader
             BufferedReader br = new BufferedReader(txtReader);// 建立BufferedReader對象,并實例化為br
             String Line = br.readLine();// 從文件讀取一行字符串

             // 判斷讀取到的字符串是否不為空
             for (int i = 0; Line != null; i++) {
              // System.out.println(Line);//輸出從文件中讀取的數據
              array.add(Line);
              Line = br.readLine();// 從文件中繼續讀取一行數據
             }
             DataOfLine = new String[array.size()];
             // System.out.println(array.size());
             array.toArray(DataOfLine);
             br.close();// 關閉BufferedReader對象
             txtReader.close();// 關閉文件
            } catch (FileNotFoundException e1) {
             e1.printStackTrace();
            } catch (IOException e1) {
             e1.printStackTrace();
            }

            try {
             // 創建新的Excel 工作簿
             HSSFWorkbook workbook = new HSSFWorkbook();
             // 在Excel工作簿中建一工作表,其名為缺省值
             // 如要新建一名為"效益指標"的工作表,其語句為:
             // HSSFSheet sheet = workbook.createSheet("效益指標");
             HSSFSheet sheet = workbook.createSheet("FeeOfTel");

             // 創建字體,設置其為紅色、粗體
             HSSFFont font = workbook.createFont();
             font.setColor(HSSFFont.COLOR_NORMAL);
             font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
             HSSFCellStyle cellStyle = workbook.createCellStyle();
             //將字體設置到中間
             cellStyle.setFont(font);
             cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
             //將文字設置到右邊
             HSSFCellStyle cellStyle1 = workbook.createCellStyle();
             cellStyle1.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
          //    HSSFDataFormat format = workbook.createDataFormat();
          //    cellStyle1.setDataFormat(format.getFormat("0.00"));

             int NumLine = (array.size() / 13);
             System.out.println(NumLine);
             // 在索引0的位置創建行(最頂端的行)
             HSSFRow row = null;
             HSSFCell cell = null;
             for (int j = 0; j <= NumLine; j++) {
              row = sheet.createRow((short) j);
              for (int i = 0; i <= 12 && i + j * 13 < array.size(); i++) {
               // 在索引0的位置創建單元格(左上端
               cell = row.createCell((short) i);

               // 定義單元格為字符串類型
               cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
               cell.setEncoding(HSSFCell.ENCODING_UTF_16);
          //     cell.setCellType(HSSFCell.CELL_TYPE_STRING);
               if (j == 0 || j == NumLine - 1) {
                cell.setCellStyle(cellStyle);
                cell.setCellValue(DataOfLine[i + j * 13]);
               }
               else{
                cell.setCellStyle(cellStyle1);
                cell.setCellValue(DataOfLine[i + j * 13]);
               }
               // 在單元格中輸入一些內容
               // System.out.println(DataOfLine[i + j * 13]);
          //     cell.setCellValue(DataOfLine[i + j * 13]);
              }
             }

             // 新建一輸出文件流
             FileOutputStream fOut = new FileOutputStream(outputFile);
             // 把相應的Excel 工作簿存盤
             workbook.write(fOut);
             fOut.flush();
             // 操作結束,關閉文件
             fOut.close();
             System.out.println("文件生成...");
            } catch (Exception e) {
             System.out.println("已運行 TxtToExecl() : " + e);
            }
           }
          }

          posted on 2005-12-12 12:58 Ben 閱讀(713) 評論(4)  編輯  收藏

          評論

          # re: java中利用POI處理Excel 2006-03-21 14:04 dvpud

          邁康科技是家從事<a href="http://www.sphy.com.cn">視頻會議</a>,<a href="http://www.menjin-kaoqin.cn">門禁</a>,<a href="http://www.menjin-kaoqin.cn">考勤</a>業務的公司,在網絡<a href="http://www.vpn-firewall.com.cn">防火墻</a>,<a href="http://www.vpn-firewall.com.cn">VPN</a>工程方面有著豐富的經驗, 在樓宇<a href="http://www.jiankong-anfang.cn">安防</a> , <a href="http://www.jiankong-anfang.cn">監控</a> 等方面也有豐富案例. 并有免費的<a href="http://www.shipinhuiyi.com.cn">視頻會議</a>光盤贈送. 免費自助<a href="http://www.markcom.cn">網站建設</a>為您服務.
          咨詢電話: 021 - 51089657.  回復  更多評論   

          # re: java中利用POI處理Excel 2006-03-21 14:04 dvpud

          上海邁康科技是一家專業從事[url=http://guoqingfu.blog.ccidnet.com]視頻會議[/url]系統,瑞福特<a href="http://guoqingfu.blog.ccidnet.com">視頻會議</a>系統的企業,從事[url=http://www.blogcn.com/user89/markcom/index.html]視頻會議[/url]產品工程,<a href="http://www.blogcn.com/user89/markcom/index.html">視頻會議</a>系統集成. [url=http://markfu.cn.etlong.com]視頻會議[/url]的系統集成.<a href="http://markfu.cn.etlong.com">視頻會議</a>. 咨詢電話: 021 - 51089657.
            回復  更多評論   

          # re: java中利用POI處理Excel 2006-05-10 15:42 dvpud

          [url=http://www.blogcn.com/u/81/17/shxspin/index.html]LED顯示屏[/url]
          [url=http://shxspin.blog.hexun.com/]LED顯示屏[/url]
          [url=http://shxspin.blog.163.com]LED顯示屏[/url]
          [url="http://shxspin.blog.com.cn]LED顯示屏[/url]
          [url="http://shxspin.blog.ccidnet.com]LED顯示屏[/url]
          [url=http://blog.hc360.com/shxspin]LED顯示屏[/url]
          [url=http://blogger.org.cn/blog/blog.asp?name=shxspin]LED顯示屏[/url]
          [url=http://blog.donews.com/shxspin/]LED顯示屏[/url]
            回復  更多評論   

          # re: java中利用POI處理Excel 2006-05-10 15:43 dvpud

          [url=http://chenjiaying.blog.ccidnet.com]VPN[/url]
          [url=http://chenjiaying.blog.hexun.com]VPN[/url]
          [url=http://chenjiayingvpn.blog.tom.com]VPN[/url]
          [url=http://firewall.cn.etlong.com]VPN[/url]
          [url=http://www.vpn-firewall.com.cn]VPN[/url]
          [url=http://markcom.cn.etlong.com/]鋼絲繩[/url]
          [url=http://www.blogcn.com/u/35/83/shgss/index.html]鋼絲繩[/url]
          [url=http://hexun.com/shgss]鋼絲繩[/url]
          [url=http://shgss.blog.tom.com/]鋼絲繩[/url]
          [url=http://shgss.blog.com.cn]鋼絲繩[/url]
          [url=http://www.blogcn.com/u/35/83/shgss/index.html]鋼絲繩[/url]
          [url=http://www1.blog.163.com/-ZGeV.html]鋼絲繩[/url]  回復  更多評論   


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


          網站導航:
           
          主站蜘蛛池模板: 凉城县| 庆云县| 阳春市| 宝鸡市| 化德县| 娱乐| 乌拉特中旗| 彰化县| 米林县| 介休市| 桃园县| 新竹县| 雅江县| 云和县| 山阳县| 竹山县| 东辽县| 巧家县| 富锦市| 北辰区| 南阳市| 台中市| 乌拉特后旗| 江川县| 香港| 京山县| 山东| 柳江县| 湛江市| 资讯 | 邻水| 沂南县| 长寿区| 图木舒克市| 宜章县| 金堂县| 梁河县| 确山县| 长沙市| 吉林市| 呼和浩特市|