עבור לתוכן

C# רוצה להפוך דוח rdlc לקובץ PDF.

Featured Replies

פורסם

Response - לאיזה using System שייך??

(מה אני צריכה להגדיר כדי שיזוהה בקוד?)

פורסם

קשה לומר בלי לדעת כלום על הקוד שלך. מאיפה מגיע ה-Response הזה? מה את עושה איתו?

נ.ב. תערכי בבקשה את הכותרת למשהו יותר אינפורמטיבי.

פורסם
  • מחבר

אני רוצה להפוך דוח rdlc לקובץ PDF.

קוד:

 
LocalReport localReport = new LocalReport();
localReport.ReportPath = Server.MapPath("~/Report.rdlc");

//A method that returns a collection for our report
//Note: A report can have multiple data sources
List<Employee> employeeCollection = GetData();

//Give the collection a name (EmployeeCollection) so that we can reference it in our report designer
ReportDataSource reportDataSource = new ReportDataSource("EmployeeCollection", employeeCollection);
localReport.DataSources.Add(reportDataSource);

string reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;

//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>PDF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";

Warning[] warnings;
string[] streams;
byte[] renderedBytes;

//Render the report
renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);

//Clear the response stream and write the bytes to the outputstream
//Set content-disposition to "attachment" so that user is prompted to take an action
//on the file (open or save)
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=foo." + fileNameExtension);
Response.BinaryWrite(renderedBytes);
Response.End();

מאיפה מגיע הRESPONSE? כי כרגע הוא לא מזוהה לי עם קו אדום מתחתיו :nixweiss:

פורסם

אם תחפשי קצת בגוגל את הפונקציות שהוא קורא להן (Response.Clear, Response.End לדוגמה) אז תגלי שזה חלק מ-ASP או ASP.NET.

מאיפה העתקת את הקוד הזה?

ושוב:

נ.ב. תערכי בבקשה את הכותרת למשהו יותר אינפורמטיבי.

פורסם

זה אומר שאת צריכה לקרוא ולהבין את הקוד, ולא סתם להעתיק אותו כמו שהוא...

החלק של ה-Response בסוף לא קשור בכלל להמרה ל-PDF. זה רק מה שמחזיר את התוצאה למשתמש (ב-ASP התוצאה חוזרת כ-Response). את יכולה לחפש עוד דוגמאות להמרה מ-RDLC ל-PDF בשביל להבין יותר טוב מה את אמורה לעשות.

לחילופין, את יכולה לנסות להבין מהקוד איפה הקטע שבו מתבצעת ההמרה עצמה, ואז לחפש את הפונקציה הספציפית הזו בגוגל כדי למצוא דוגמאות שימוש בה.

פורסם
  • מחבר

ההמרה מתבצעת ע"י המטודה Render?

פורסם
  • מחבר

אז איפה הטעות בקוד שהוא נופל לי בשורה הזו?

 

Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>PDF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>"; byte[] bytes = localReport.Render("PDF", deviceInfo, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);

מתקבלת השגיאה: An error occurred during local report processing.

פורסם

תדבגי ותעקבי אחרי השגיאה. תעטפי את הקוד שלך ב-try/catch, תדפיסי את השגיאה או תדבגי ותסתכלי עליה (ועל ה-inner exception שלה).

חוץ מזה, תחפשי עוד דוגמאות להמרה מ-rdlc ל-pdf.

פורסם
  • מחבר

כשאני שמה את השורה הזאת בTRY, איך נראה הCATCH?

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים