我需要将txt文件转换为一个Excel文件,并进一步格式化每个工作簿。 将所有txt文件转换为excel已完成,但问题是根据其行和列格式化每个工作表。例如,在第一张表中需要合并(0,0,14,18),在第二张表中需要合并(1, 2,0,0)。我尝试使用sheet.addMergedRegion(new CellRangeAddress(0,0,14,17));但它将合并所有工作表的相同单元格。下面是我的代码plz帮助。
package csv2excell;
import java.io.*;
import java.util.ArrayList;
import org.apache.poi.hssf.model.Sheet;
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;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
public class csv2excell{
private static void printUsage(int errorCode, String string) {
System.out.println("Usage: <scriptName> csvPath outputFileName csvfileName1 csvfileName2 and so on....");
if(errorCode == 1) System.out.println("Incorrect number of arguments, script will now exit");
if(errorCode == 2) System.out.println("Specified directory "+string+" does not exist, script will now exit");
if(errorCode == 3) System.out.println("Error reading files from "+string+" , script will now exit");
System.exit(-1);
}
private static void makeRowBold(HSSFWorkbook wb, Row row){
CellStyle style = wb.createCellStyle();//Create style
Font font = wb.createFont();//Create font
style.setAlignment(CellStyle.ALIGN_CENTER);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);//Make font bold
style.setFont(font);//set it to bold
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row
row.getCell(i).setCellStyle(style);//Set the style
}
}
public static void main(String[]args){
if(args.length < 3) printUsage(1,null);
try{
File folder = new File(args[0]);
if(!folder.isDirectory()) printUsage(2,args[0]);
File[] listOfFiles = new File[args.length-2];
for (int i=2;i<args.length;i++) {
listOfFiles[i-2] = new File(args[0]+"/"+args[i]);
// System.out.println("Reading "+args[i]);
}
for(int i=0;i<listOfFiles.length;i++) {
String filename = (listOfFiles[i].getName());
//System.out.println(filename);
}
HSSFWorkbook workbook=new HSSFWorkbook();
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();//Create font
font.setBoldweight(Font.BOLDWEIGHT_BOLD);//Make font bold
style.setFont(font);//set it to bold
for (File file : listOfFiles) {
if (file.isFile()) {
String thisline;
ArrayList<String> al = null;
ArrayList<ArrayList<String>> arlist = new ArrayList<ArrayList<String>>();
HSSFSheet sheet = workbook.createSheet(file.getName());
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
int LineNum=0;
while ((thisline = br.readLine()) != null) {
//System.out.println("DEBUG: "+file.getName()+":"+thisline);
al = new ArrayList<String>();
String strar[] = thisline.split(",");
for (int j = 0; j < strar.length; j++) {
//System.out.println("here");
al.add(strar[j]);
}
arlist.add(al);
for (int k = 0; k < arlist.size(); k++) {
//System.out.println("here1");
ArrayList<String> ardata = (ArrayList<String>) arlist.get(k);
HSSFRow row = sheet.createRow((short) k);
for (int p = 0; p < ardata.size(); p++) {
HSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
if(p == 0) {
cell.setCellStyle(style);
//System.out.println(LineNum+":"+p);
}
}
}
/* System.out.println("DEBUG: arlist size before"+arlist.size());
arlist.add(al);
System.out.println("DEBUG: arlist size after"+arlist.size());*/
// LineNum++;
}
makeRowBold(workbook, sheet.getRow(0));
fis.close();
FileOutputStream fileOut = new FileOutputStream(args[0]+"/"+args[1]);
sheet.addMergedRegion(new CellRangeAddress(0,0,14,17));
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
br.close();
}
}
System.out.println("Your excel file has been generated at "+args[0]+"/"+args[1]+"!");
} catch ( Exception ex ) {
System.out.println(ex);
}
}
private static void printIn(boolean filenumber) {
// TODO Auto-generated method stub
}
答案 0 :(得分:0)
sheet.addMergedRegion(new CellRangeAddress(0,0,14,17));
循环中有for
。这将合并您正在创建的所有工作表的这些范围。
您需要添加一个验证(例如计算迭代的简单变量i
),并合并每次迭代所需的范围。
在你的情况下,它可能是
if(i==0){
//first sheet
sheet.addMergedRegion(new CellRangeAddress(0,0,14,17));
} else if (i==1){
//second sheet
sheet.addMergedRegion(new CellRangeAddress(1,2,0,0));
}