将 csv 转换为 xls 时出现宏问题
我的项目遇到问题。有一个 xls 文件,其中包含一个创建图表的宏,该 xls 文件的单元格类型是“文本”。我必须使用该宏制作该文件的多个副本,然后从一个名为“结果”的文件夹,我必须获取 csv 文件并将值一一放入这些重复的 xls 文件中,并通过宏我必须创建与这些值相对应的图表。我知道如何从 csv 获取值并将其放入一份.xls文件。我还知道可以创建图表的宏的代码。
但问题是,当我将值发送到重复文件时,它不会显示宏。我正在使用下面所示的代码。
任何人都可以修改我的代码以获取 csv 文件的值到那些可以显示宏的重复 xls 文件。请帮助我。
提前THNX......
包com.hp.io;
导入 java.io.BufferedInputStream; 导入 java.io.BufferedOutputStream; 导入 java.io.BufferedReader; 导入java.io.File; 导入 java.io.FileInputStream; 导入 java.io.FileOutputStream; 导入 java.io.FilenameFilter; 导入java.io.IOException; 导入 java.io.InputStreamReader; 导入java.util.ArrayList; 导入 org.apache.poi.hssf.usermodel.HSSFCell; 导入 org.apache.poi.hssf.usermodel.HSSFRow; 导入 org.apache.poi.hssf.usermodel.HSSFSheet; 导入 org.apache.poi.hssf.usermodel.HSSFWorkbook;
公共类 CSVtoXLS {
// FileOutputStream fileOut=null;
//FileInputStream fis=null;
公共静态 void main(String[] args) 抛出 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;
}
}
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;
import 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎每次都会创建一个新的空 Excel 文件
假设我正确理解您的问题,您将需要打开一个现有的 Excel 文件,其中已定义了宏。然后将 CSV 中的数据写入该文件,最后保存。然后您将获得一个包含宏和数据的 Excel 文件。
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.