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);
}
}
}