PDF にエクスポートする値がいくつかありますが、Struts でこれを行うにはどうすればよいですか?
ユーザーがエクスポートされた PDF ファイルをダウンロードするためのダウンロード オプションを取得するよりも、ユーザーがボタンまたはリンクをクリックすると、オプション ボタンまたはリンクが表示されます。
PDF にエクスポートする必要がある結果セットがあります。
どうすればいいのか教えてください。
よろしく
PDF にエクスポートする値がいくつかありますが、Struts でこれを行うにはどうすればよいですか?
ユーザーがエクスポートされた PDF ファイルをダウンロードするためのダウンロード オプションを取得するよりも、ユーザーがボタンまたはリンクをクリックすると、オプション ボタンまたはリンクが表示されます。
PDF にエクスポートする必要がある結果セットがあります。
どうすればいいのか教えてください。
よろしく
このアクションクラスの例を見てください。
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.lowagie.text.pdf.*;
import com.lowagie.text.*;
import java.io.*;
public class download extends org.apache.struts.action.Action {
/* forward name="success" path="" */
private static final String SUCCESS = "success";
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Document document=new Document();
System.out.println(clientIp);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=temp.pdf");
try
{
OutputStream out = response.getOutputStream();
PdfWriter.getInstance(document,out);
document.open();
document.add(new Paragraph("Hello Pdf"));
document.close();
}
finally
{
return mapping.findForward(SUCCESS);
}
}
それに応じて struts-config.xml も更新します。
これは、あなたが望むことをするのに役立つかもしれない例です:
import java.io.*;
import java.sql.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class CreatePDF{
public static void main(String arg[])throws Exception{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("C:/data.pdf"));
document.open();
PdfPTable table = new PdfPTable(2);
table.addCell("Name");
table.addCell("Address");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test",
"root", "root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from data");
while(rs.next()) {
table.addCell(rs.getString("name"));
table.addCell(rs.getString("address"));
}
document.add(table);
document.close();
}
}
itext.jar
ただし、実装する前に、フォルダーに入れる必要がありWEB-INF/lib
ます。また、pdf ファイルを操作する方法もたくさん見つかりました。