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 阅读(699) 评论(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">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">http://www.blogcn.com/u/35/83/shgss/index.html]钢丝绳[/url]
[url=http://www1.blog.163.com/-ZGeV.html]钢丝绳[/url]  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: