有没有将TXT转换EXCEl的小程序?急

treesofbayou 2010-12-17 02:08:41
有没有将TXT转换EXCEl的小程序?急
...全文
123 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxr123 2010-12-20
  • 打赏
  • 举报
回复
package com.sitech.tools.parseexcel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class CreateBossExcel {

/**
* @param args
*/
private static final String path = "D://youhua//yangshuang";

public void createExcel(String path) throws IOException {
File file = new File(path);

if (file.exists() && file.isDirectory()) {
File[] subFile = file.listFiles();
for (int j = 0; j < subFile.length; j++) {
create(subFile[j], path);
System.out
.println("*********************************************"
+ subFile[j].getName());
}
} else {
System.out.println(path + "does not exist!");
}
}

private void create(File subfile, String path) throws IOException {
String filename = subfile.getName() + ".xls";
FileOutputStream fos = new FileOutputStream(filename);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(0, filename);

HSSFRow row = null;
HSSFCell cell = null;

row = sheet.createRow(0);

cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("定时时间");
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("定时代码路径");
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("备注");
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("添加人员");
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("是否涉及数据库");
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("是否需要修改");

LineNumberReader lnr = new LineNumberReader(new FileReader(subfile));
String line = null;
int ttt = 1;
String str1 = "";
String str2 = "";
String str3 = "";
while ((line = lnr.readLine()) != null){
if(line.startsWith("#")){
str1 = line.substring(line.indexOf("#"));
System.out.println(str1);
}else{
if(line.lastIndexOf("*") == -1){
}else{
str2 = line.substring(0,line.indexOf("*"));
str3 = line.substring(line.indexOf("/"),line.indexOf(".sh"));
}
}

if(!str1.equals("") && !str2.equals("") && !str3.equals("")){
row = sheet.createRow(ttt);
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(str2);
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(str3+".sh");
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(str1);
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(" ");
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(" ");
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(" ");
ttt++;
str1 = "";
str2 = "";
str3 = "";
}
}
System.out.println("ttt = "+ttt);
wb.write(fos);
fos.close();
}

public static void main(String[] args) throws IOException {
CreateBossExcel cbe = new CreateBossExcel();
cbe.createExcel(path);
}

}

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧