⑴ 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();
}
}
}
}
}
簡單的寫了下。 我機器上可以運行