📄 poi导出图片.txt
字号:
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
//HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 0,0,(short)10,10);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 0,15,(short)10,20);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("d:/workbook.xls");
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();
}
}
}
一个sheep是可以插入多个图片的,看以下代码:
java代码:
public class TestPOI {
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg =null;
BufferedImage bufferImg1 = null;
try{
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("d:/ok.jpeg"));
bufferImg1 = ImageIO.read(new File("d:/ok1.jpeg"));
ImageIO.write(bufferImg,"jpeg",byteArrayOut);
ImageIO.write(bufferImg1,"jpeg",byteArrayOut1);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
//HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 0,0,(short)10,10);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 0,15,(short)10,20);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("d:/workbook.xls");
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();
}
}
}
一个sheep是可以插入多个图片的,看以下代码:
java代码:
public class TestPOI {
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg =null;
BufferedImage bufferImg1 = null;
try{
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("d:/ok.jpeg"));
bufferImg1 = ImageIO.read(new File("d:/ok1.jpeg"));
ImageIO.write(bufferImg,"jpeg",byteArrayOut);
ImageIO.write(bufferImg1,"jpeg",byteArrayOut1);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
//HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 0,0,(short)10,10);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 0,15,(short)10,20);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("d:/workbook.xls");
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();
}
}
}
一个sheep是可以插入多个图片的,看以下代码:
java代码:
public class TestPOI {
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg =null;
BufferedImage bufferImg1 = null;
try{
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("d:/ok.jpeg"));
bufferImg1 = ImageIO.read(new File("d:/ok1.jpeg"));
ImageIO.write(bufferImg,"jpeg",byteArrayOut);
ImageIO.write(bufferImg1,"jpeg",byteArrayOut1);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
//HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 0,0,(short)10,10);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 0,15,(short)10,20);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("d:/workbook.xls");
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();
}
}
}
一个sheep是可以插入多个图片的,看以下代码:
java代码:
public class TestPOI {
public static void main(String[] args) {
FileOutputStream fileOut = null;
BufferedImage bufferImg =null;
BufferedImage bufferImg1 = null;
try{
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File("d:/ok.jpeg"));
bufferImg1 = ImageIO.read(new File("d:/ok1.jpeg"));
ImageIO.write(bufferImg,"jpeg",byteArrayOut);
ImageIO.write(bufferImg1,"jpeg",byteArrayOut1);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
//HSSFRow row = sheet1.createRow(2);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 0,0,(short)10,10);
HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,512,255,(short) 0,15,(short)10,20);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut = new FileOutputStream("d:/workbook.xls");
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();
}
}
}
jasperreports报表(实例)
IT小混混 发表于 2006-3-13 14:24:00
1.建立数据库联线。
2.由sql語法取出資料,准备好传入的 parameters。
3.將parameters及取出的資料傳入Jasper中。會由 .jasper檔產生出 .jrprint 檔。
4.再由 .jrprint 檔產生出 .pdf的目的檔。
//建立连线方法
public Connection createConnection(){
Connection conn;
try{
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url=
"jdbc:microsoft:sqlserver://10.0.0.151:1433;DatabaseName=Northwind";
Class.forName(driver);
conn = DriverManager.getConnection(url, "sa", "sa"); //建立连线
conn.setAutoCommit(false);
return conn;
}
catch(SQLException ex1){
System.out.println("建立连线錯誤="+ex1.toString());
}
catch (ClassNotFoundException ex2){
System.out.println("建立连线錯誤="+ex2.toString());
}
return null;
}
//关闭资料连线
public void CloseConnect(Connection conn){
try{
conn.commit();
conn.setAutoCommit(true);
conn.close();
}
catch(Exception e){
System.out.println("连线关闭错误="+e.toString());
}
}
//建立PDF輸出
public void createPdfReport(){
String fileName="";
String strDate="";
String sql="";
//取得今天日期
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
strDate=String.valueOf(cal.get(Calendar.YEAR))+"-";
if(cal.get(Calendar.MONTH)<9)
strDate+="0";
strDate+=String.valueOf(cal.get(Calendar.MONTH)+1)+"-";
if(cal.get(Calendar.DATE)<9)
strDate+="0";
strDate+=String.valueOf(cal.get(Calendar.DATE));
Map parameters=new HashMap();
parameters.put("rptToday",strDate); //傳入的parameters引數
sql =" select CompanyName ,ContactName , ";
sql+=" ContactTitle , City , Country ";
sql+=" from Suppliers ";
sql+=" where ContactTitle like '%Sales%' "; //在此試捉出與Sales有關
sql+=" order by Country , ContactTitle ";
Connection conn = this.createConnection(); //建立資料庫連線
try {
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(sql);
fileName = "D:\\iReport\\iReport-0.4.0\\Work\\TestiReport.jasper";
JasperFillManager.fillReportToFile(fileName,parameters,
newJRResultSetDataSource(rs));
fileName = "D:\\iReport\\iReport-0.4.0\\Work\\TestiReport.jrprint";
JasperExportManager.exportReportToPdfFile(fileName);
}
catch (Exception ex) {
System.out.println("Error : "+ex.toString());
}
finally {
CloseConnect(conn);
conn = null;
}
}
由createConnection、CloseConnect 及createPdfReport 这三个方法,即可
产生出一個 PDF 档。只要将這三個方法加入至你的应用程序里,要产生PDF时,即
呼叫createPdfReport()這個方法即可。注意你要传递的参数和sql语句
希望对你有用
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -