⑴ java怎么导出excel 像下面图片那种样式的 跪求大神指导下
思路如下:
1、把上述Excel中的数据删除掉,保存为一个模版。
2、使用POI、JXL等工具读入模版。
3、按业务逻辑填充模版。
4、将填充好的模版另存为Excel。
⑵ 请问下java中导出图片怎么做
很简单的啦,bufferedimage啦你去看看jdk的demo吧
⑶ JAVA如何将JFreeChart图片导出到Excel
我才做了这个,你看看行不行,我是把生成的图片放到一个临时文件中,然后再用poi将这个图片导入到excel中,代码如下:
package jfreechart;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class testBarChart2 extends ActionSupport{
JFreeChart chart;
HttpSession session;
HttpServletRequest request;
@Override
public String execute() throws Exception {
request=ServletActionContext.getRequest();
session=request.getSession();
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
dataset.addValue(1000,"广州","猪肉");
dataset.addValue(220,"广州","牛肉");
dataset.addValue(530,"广州","鸡肉");
dataset.addValue(340,"广州","鱼肉");
chart=ChartFactory.createBarChart3D("肉类销量统计图","肉类","销量",dataset,PlotOrientation.VERTICAL,false,false,false);
Font font=new Font("宋体",Font.BOLD,20);
CategoryPlot plot=(CategoryPlot)chart.getPlot();
TextTitle textTitle=chart.getTitle();
textTitle.setFont(font);//设置标题的字体
CategoryAxis domainAxis=plot.getDomainAxis();//柱状图的x轴
domainAxis.setTickLabelFont(font);//设置x轴坐标上的字体
domainAxis.setLabelFont(font);//设置x轴上的标题的字体
ValueAxis valueAxis=plot.getRangeAxis();//柱状图的y轴
valueAxis.setTickLabelFont(font);//设置y轴坐标上的字体
valueAxis.setLabelFont(font);//设置y轴坐标上的标题的字体
/**添加上下面的语句会在临时文件夹下面生成图片,去掉就不会有了*/
String filename="E:/tomcat/basicsms/apache-tomcat-6.0.18/temp/"+ServletUtilities.saveChartAsPNG(chart,500,300,null,session);
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
try {
// 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File(filename));
ImageIO.write(bufferImg, "png", byteArrayOut);
// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 512, 255,
(short) 1, 1, (short) 10, 20);
anchor.setAnchorType(2);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut
.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)).resize(0.8);
fileOut = new FileOutputStream("d:/workbook.xls");
// 写入excel文件
wb.write(fileOut);
fileOut.close();
} catch (IOException io) {
io.printStackTrace();
System.out.println("io erorr : " + io.getMessage());
} finally {
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return SUCCESS;
}
public JFreeChart getChart() {
return chart;
}
}
当执行action的时候,图片会在页面显示,也会导入到workbook.xls中,这个例子其实是我在网上找的,然后拼写成的,希望对你有帮助,我目前也在研究当中,也可以共同讨论
其实你要也可以试试jacob,它可以直接在excel中生成图片,你可以自己研究研究
⑷ java导出excel图表
通过Java程序导出带图表的excel吗?参考下面用spire.xls.jar来创建Excel图表的方法,这里以创建饼图为例,当然你也可以指定创建其他图表类型,如柱状图、折线图、雷达图、散点图等等:
import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;
import java.awt.*;
public class CreatePieChart {
public static void main(String[] args) {
//创建Workbook对象
Workbook workbook = new Workbook();
//获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//将图表数据写入工作表
sheet.getCellRange("A1").setValue("年份");
sheet.getCellRange("A2").setValue("2002");
sheet.getCellRange("A3").setValue("2003");
sheet.getCellRange("A4").setValue("2004");
sheet.getCellRange("A5").setValue("2005");
sheet.getCellRange("B1").setValue("销售额");
sheet.getCellRange("B2").setNumberValue(4000);
sheet.getCellRange("B3").setNumberValue(6000);
sheet.getCellRange("B4").setNumberValue(7000);
sheet.getCellRange("B5").setNumberValue(8500);
//设置单元格样式
sheet.getCellRange("A1:B1").setRowHeight(15);
sheet.getCellRange("A1:B1").getCellStyle().setColor(Color.darkGray);
sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
sheet.getCellRange("A1:B1").getCellStyle().setVerticalAlignment(VerticalAlignType.Center);
sheet.getCellRange("A1:B1").getCellStyle().setHorizontalAlignment(HorizontalAlignType.Center);
sheet.getCellRange("B2:C5").getCellStyle().setNumberFormat(""¥"#,##0");
//添加饼图
Chart chart = sheet.getCharts().add(ExcelChartType.Pie);
//设置图表数据区域
chart.setDataRange(sheet.getCellRange("B2:B5"));
chart.setSeriesDataFromRange(false);
//设置图表位置
chart.setLeftColumn(3);
chart.setTopRow(1);
chart.setRightColumn(11);
chart.setBottomRow(20);
//设置图表标题
chart.setChartTitle("年销售额");
chart.getChartTitleArea().isBold(true);
chart.getChartTitleArea().setSize(12);
//设置系列标签
ChartSerie cs = chart.getSeries().get(0);
cs.setCategoryLabels(sheet.getCellRange("A2:A5"));
cs.setValues(sheet.getCellRange("B2:B5"));
cs.getDataPoints().getDefaultDataPoint().getDataLabels().hasValue(true);
chart.getPlotArea().getFill().setVisible(false);
//保存文档
workbook.saveToFile("output/PieChart.xlsx", ExcelVersion.Version2016);
}
}
饼图创建效果:
excel饼状图效果
⑸ java 导出Excel数据如何生成图表,如柱状图,折线图
我以前做的是利用jfreechart生成图片,导出excel的时候把图片放进去。
⑹ 请教java导出多张图片到Excel问题!
package tei;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestExcelImage {
static List<BufferedImage> images = new ArrayList<>();
static {
try {
images.add(ImageIO.read(new File("C:/t/1.jpg")));
images.add(ImageIO.read(new File("C:/t/2.jpg")));
images.add(ImageIO.read(new File("C:/t/3.jpg")));
images.add(ImageIO.read(new File("C:/t/4.jpg")));
images.add(ImageIO.read(new File("C:/t/5.jpg")));
images.add(ImageIO.read(new File("C:/t/6.jpg")));
images.add(ImageIO.read(new File("C:/t/7.jpg")));
images.add(ImageIO.read(new File("C:/t/8.jpg")));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileOutputStream fileOut = null;
try {
// 创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
// HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
short i = 0;
for (BufferedImage image : images) {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOut);
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1 + i, (short) 2, 2 + i);
anchor.setAnchorType(0);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
i++;
}
HSSFRow row = sheet1.createRow(10);
short s = 10;
HSSFCell cell = row.createCell(s);
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setStrikeout(true);
style.setFont(font);
cell.setCellStyle(style);
cell.setCellValue("aaaaa");
fileOut = new FileOutputStream("c:/workbook.xls");
// 写入excel文件
wb.write(fileOut);
fileOut.close();
} catch (IOException io) {
io.printStackTrace();
System.out.println("io erorr : " + io.getMessage());
} finally {
if (fileOut != null) {
try {
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
简单的写了下。 我机器上可以运行