我正在尝试从 excel 中读取数据并使用 java 绘制图形。
我不断收到以下错误:
org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供的数据似乎在 Office 2007+ XML 中。您正在调用处理 OLE2 Office 文档的 POI 部分。您需要调用 POI 的不同部分来处理此数据(例如 XSSF 而不是 HSSF)
线程“主”java.lang.NoClassDefFoundError 中的异常:org/jfree/util/PublicCloneable
我的代码:
import java.io.*;
import java.util.*;
import org.jfree.data.*;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.plot.PlotOrientation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jfree.data.category.DefaultCategoryDataset;
public class ReadExcel{
public static void main(String[]args){
short a=0;
short b=1;
int i=0;
ArrayList<Integer> list1=new ArrayList<Integer>();
ArrayList<Integer> list2=new ArrayList<Integer>();
int x=0, y=0;
String filename ="C:\\Chart.xlsx";
if(filename != null && !filename.equals("")){
try{
FileInputStream fs =new FileInputStream(filename);
HSSFWorkbook wb = new HSSFWorkbook(fs);
for(int k = 0; k < wb.getNumberOfSheets(); k++){
int j=i+1;
HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows();
for(int r = 1; r < rows; r++){
HSSFRow row = sheet.getRow(r);
int cells = row.getPhysicalNumberOfCells();
HSSFCell cell1 = row.getCell(a);
x =(int) cell1.getNumericCellValue();
HSSFCell cell2 = row.getCell(b);
y =(int) cell2.getNumericCellValue();
list1.add(new Integer(x));
list2.add(new Integer(y));
}
i++;
}
}catch(Exception e){
System.out.println(e);
}
}
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for(int j=0;j<list1.size();j++){
dataset.setValue((double)list2.get(j), "Marks", list1.get(j).toString());
}
JFreeChart chart = ChartFactory.createBarChart("BarChart using JFreeChart","ID", "Marks", dataset,
PlotOrientation.VERTICAL, false,true, false);
try {
ChartUtilities.saveChartAsJPEG(new File("C:\\chart.jpg"), chart,400, 300);
} catch (IOException e) {
System.out.println("Problem in creating chart.");
}
}
}
示例excel:
Response Time Transaction Name Runid
25 Home 1
56 Login 2
23 Order 3
我尝试使用 XSSF Apache POI 但没有运气。
我还想将图表的图像保存在本地某处。