我试图将jrxml文件导出为pdf,但我收到此错误:
WARN query.JRJdbcQueryExecuter - The supplied java.sql.Connection object is null.
我只得到一个空白的pdf文件..
这是我导出为PDF的方法:
public void printReport(ActionEvent event) throws JRException, IOException {
String reportPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/test.jrxml");
JasperReport report = JasperCompileManager.compileReport(reportPath);
JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<String, String>());
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
FacesContext.getCurrentInstance().responseComplete();
}
我是jasperreports的新手,所以我有点失落..我必须指定连接字符串到de数据库或什么?我应该在哪里添加它。
BTW,我正在使用JSF 2,intellij和maven。
感谢。
答案 0 :(得分:11)
我解决了我的问题..我必须为我的报告指定数据库连接!
Connection conn;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:55334;databaseName=Frutemu;integratedSecurity=true","","");
} catch (SQLException ex) {
} catch (ClassNotFoundException ex) {
}
然后在此行中添加连接:
JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<String, String>(), conn);
答案 1 :(得分:0)
如果要使用Java bean作为源而不是DB连接,则可以这样做以避免异常:
Sub Test()
GetExcel_INV_Hist "C:\Path_To_My_Workbook\Book1.xlsx"
End Sub
Public Sub GetExcel_INV_Hist(File_Path_Name As String)
Dim MyXL As Object
Dim MyWB As Object
Set MyXL = CreateXL
Set MyWB = MyXL.Workbooks.Open(File_Path_Name)
With MyWB.worksheets("Sheet1")
.Columns("AA:AC").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
.Columns("Z:Z").NumberFormat = "0.00%"
With .Columns("Z:Z")
.FormatConditions.Add Type:=2, Formula1:="=$Y1=""GM""" '2 = xlExpression
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1)
.NumberFormat = "$#,##0.00"
End With
End With
End With
End Sub
Public Function CreateXL(Optional bVisible As Boolean = True) As Object
Dim oTmpXL As Object
On Error Resume Next
Set oTmpXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
On Error GoTo ERROR_HANDLER
Set oTmpXL = CreateObject("Excel.Application")
End If
oTmpXL.Visible = bVisible
Set CreateXL = oTmpXL
On Error GoTo 0
Exit Function
ERROR_HANDLER:
MsgBox "Error " & Err.Number & vbCr & _
" (" & Err.Description & ") in procedure CreateXL."
End Function