0
我需要將txt文件轉換爲一個Excel文件並進一步格式化每個工作簿。 將所有txt文件轉換爲excel已完成,但問題是要根據其行和列格式化每個工作表。例如,在第一個工作表中需要合併(0,0,14,18),在第二個工作表中需要合併(1 ,2,0,0)。我嘗試使用sheet.addMergedRegion(new CellRangeAddress(0,0,14,17));但它會合並所有工作表中的相同單元格。下面是我的代碼plz的幫助。Excell文件的Java代碼
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
}