Problem with macro when converting csv to xls
I am getting a problem on my project.There is one xls file with a macro that create a chart and the cell type of that xls file is 'text'.I have to make multiple duplicate copies of that file with the macro and then from a folder named 'result' i have to fetch the csv files and put the values to those duplicate xls files one by one and through macro I have to create charts corrosponding to those values.I know how to get values from csv and put it to one xls file.I also know the codes of the macro that can create a chart.
But the problem is that when I am sending the values to the duplicate files it does not shows the macro.I am using the code shown below.
Can anybody modify my code to get the values of csv files to those duplicate xls files that can shows the macro.PLEASE HELP ME .
THNX in advance......
package com.hp.io;
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import org.apache.poi.hssf.usermodel.HSSFCell; impor开发者_StackOverflow中文版t org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class CSVtoXLS {
// FileOutputStream fileOut=null;
//FileInputStream fis=null;
public static void main(String[] args) throws IOException {
String thisline;
ArrayList<String> al = null;
ArrayList<ArrayList<String>> arlist = new ArrayList<ArrayList<String>>();
String libRoot=new File(".").getAbsolutePath();
libRoot=libRoot.replaceAll("\\\\", "/");
File f=new File(libRoot+"/result");
FilenameFilter filter = new FilenameFilter()
{
@Override public boolean accept(File dir, String name)
{
return name.endsWith(".csv");
}
};
File file[]=f.listFiles(filter);
for(int r=0;r<file.length;r++){
String r1=libRoot+"/result/output"+r+".xls";
arlist.clear();
File currentFile=file[r];
FileInputStream fis = new FileInputStream(currentFile);
//DataInputStream myInput = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while ((thisline = br.readLine()) != null) {
al = new ArrayList<String>();
String strar[] = thisline.split(",");
for (int j = 0; j < strar.length; j++) {
al.add(strar[j]);
}
arlist.add(al);
//i++;
}
fis.close();
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
for (int k = 0; k < arlist.size(); k++) {
ArrayList<String> ardata = (ArrayList<String>) arlist.get(k);
HSSFRow row = sheet.createRow((short) k);
for (int p = 0; p < ardata.size(); p++) {
//System.out.print(ardata.get(p));
HSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
}
}
//if(currentFile == file[0]){
FileOutputStream fileOut = new FileOutputStream(r1);
hwb.write(fileOut);
//hwb.write(fileOut);
//}
//hwb.write(fileOut);
fileOut.flush();
fileOut.close();
br.close();
System.out.println("conversion is done");
//hwb=null;
}
//else if (currentFile == file[1]) {
//fileOut = new FileOutputStream(libRoot+"/result/output.xls");
//hwb.write(fileOut);
//fileOut.flush();
//fileOut.close();
//hwb=null;
}
}
You appear to be creating a new, empty excel file each time
Assuming I understood your question properly, you'll want to open an existing excel file with your macro already defined in it. Then write your data from your CSV into that file, and finally save it. You'll then have an excel file with a macro and the data.
精彩评论