Wednesday, January 4, 2012

Export the documents to Excel using Xpages

The following is the code which is used to export the selected documents to excel in Xpages
var exCon = facesContext.getExternalContext();
var writer = facesContext.getResponseWriter();
var response = exCon.getResponse();
var projects:NotesView = database.getView(’EmployeeList’)
var viewNav:NotesViewNavigator = projects.createViewNav();
var viewEnt:NotesViewEntry = viewNav.getFirst();
var output:string = “”;
while (viewEnt != null) {
output += “<tr>”;
output += “<td>” + viewEnt.getColumnValues()[0]; + “</td>”;
output += “<td>” + viewEnt.getColumnValues()[1] + “</td>”;
output += “<td>” + viewEnt.getColumnValues()[2] + “</td>”;
output += “<td>” + viewEnt.getColumnValues()[3] + “</td>”;
output += “</tr>”;
viewEnt = viewNav.getNext(viewEnt);
}
response.setContentType(”application/vnd.ms-excel”);
response.setHeader(”Cache-Control”, “no-cache”);
response.setHeader(”Content-Disposition”,”attachment;filename=EmployeeDetails.xls”) ;
writer.write(”<table>”);
writer.write(”<thead><tr>”);
writer.write(”<td><b>EmployeeName</b></td>”);
writer.write(”<td><b>EmployeeId</b></td>”);
writer.write(”<td><b>Department</b></td>”);
writer.write(”<td><b>Designation</b></td>”);
writer.write(”</tr></thead>”);
writer.write(output);
writer.write(”</table>”);
writer.endDocument();

var exCon = facesContext.getExternalContext();

var writer = facesContext.getResponseWriter();

var response = exCon.getResponse();

var projects:NotesView = database.getView(’EmployeeList’)//view name

var viewNav:NotesViewNavigator = projects.createViewNav();

var viewEnt:NotesViewEntry = viewNav.getFirst();

var output:string = “”;

while (viewEnt != null) {

output += “<tr>”;

output += “<td>” + viewEnt.getColumnValues()[0]; + “</td>”;

output += “<td>” + viewEnt.getColumnValues()[1] + “</td>”;

output += “<td>” + viewEnt.getColumnValues()[2] + “</td>”;

output += “<td>” + viewEnt.getColumnValues()[3] + “</td>”;

output += “</tr>”;

viewEnt = viewNav.getNext(viewEnt);

}

response.setContentType(”application/vnd.ms-excel”);

response.setHeader(”Cache-Control”, “no-cache”);

response.setHeader(”Content-Disposition”,”attachment;filename=EmployeeDetails.xls”) ;

writer.write(”<table>”);

writer.write(”<thead><tr>”);

writer.write(”<td><b>ColumnName1</b></td>”);

writer.write(”<td><b>ColumnName2</b></td>”);

writer.write(”<td><b>ColumnName3</b></td>”);

writer.write(”<td><b>ColumnName4</b></td>”);

writer.write(”</tr></thead>”);

writer.write(output);

writer.write(”</table>”);

writer.endDocument();

Translate functionality for RICHTEXT [Change Font and Font size]

Call this agent in QuerySave of a document

@Command([EditGotoField];”Your Field Name”);

@Command([EditSelectAll]);

@Command([TextSetFontFace];”Arial”);

@Command([TextSetFontSize];”9″);

Export to Excel in Lotus Notes [LS]

The following is the Agent which is used to import the datas from excel using xpages.

1. Create an excel sheet relevant to the Form

2. Write the following code in the Agent
Sub Initialize
Print “agent is runing”
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set doc = New NotesDocument(db)
Dim One As String
Dim xlFilename As String
Dim filepath As Variant
Dim row As Integer
Dim written As Integer
‘// Connect to Excel and open the file. Start pulling over the records.
Dim Excel As Variant
Dim xlWorkbook As Variant
Dim xlSheet As Variant
‘filepath=ws.Openfiledialog(False, ” Open excel file” ,”*.xls”, “E:\New Folder”, “employeeDetails.xls”)
filepath=”E:\New Folder\Emp_Details.xls”
xlFilename=filepath
Print “File path is :” +filepath
Print “Connecting to Excel…”
Set Excel = createobject( “Excel.Application” )
Excel.Visible = False ‘// Don’t display the Excel window
print “Opening ” & xlFilename & “…”
Excel.Workbooks.Open(xlFilename) ‘// Open the Excel file
Set xlWorkbook = Excel.ActiveWorkbook
Set xlSheet = xlWorkbook.ActiveSheet
‘// Cycle through the rows of the Excel file, pulling the data over to Notes
GoTo Records
print “Disconnecting from Excel…”
xlWorkbook.Close False ‘// Close the Excel file without saving (we made no changes)
Excel.Quit ‘// Close Excel
Set Excel = Nothing ‘// Free the memory
Records:
row = 1 ‘// Integers intialize to zero
written = 0
‘MsgBox “Starting import from Excel file…”
Do While True
Finish:
With xlSheet
row = row + 1
Set doc = db.createdocument ‘// Create a new doc
doc.Form = “frm_employeeDetails”
If .Cells( row, 1 ).Value=”" Then
Exit Sub
End If
doc.name = .Cells( row, 1 ).Value
doc.department = .Cells(row, 2 ).Value
doc.designation = .Cells(row, 3).value
doc.mobno = .Cells(row, 4).Value
Call doc.Save( True, True ) ‘// Save the new doc
‘GoTo Done
written = written + 1
Print Str(written)
If .Cells( row, 1 ).Value = “” Then
GoTo Done
End If
End With
Loop
Return
Done:
print “Import Complete – Total number of documents imported —> ” & written
End Sub

Sub Initialize

Print “agent is runing”

Dim session As New NotesSession

Dim db As NotesDatabase

Dim view As NotesView

Dim doc As NotesDocument

Set db = session.CurrentDatabase

Set doc = New NotesDocument(db)

Dim One As String

Dim xlFilename As String

Dim filepath As Variant

Dim row As Integer

Dim written As Integer

‘// Connect to Excel and open the file. Start pulling over the records.

Dim Excel As Variant

Dim xlWorkbook As Variant

Dim xlSheet As Variant

‘filepath=ws.Openfiledialog(False, ” Open excel file” ,”*.xls”, “E:\New Folder”, “employeeDetails.xls”)\\Gine the Folder name here

filepath=”E:\New Folder\Emp_Details.xls”

xlFilename=filepath

Print “File path is :” +filepath

Print “Connecting to Excel…”

Set Excel = createobject( “Excel.Application” )

Excel.Visible = False ‘// Don’t display the Excel window

print “Opening ” & xlFilename & “…”

Excel.Workbooks.Open(xlFilename) ‘// Open the Excel file

Set xlWorkbook = Excel.ActiveWorkbook

Set xlSheet = xlWorkbook.ActiveSheet

‘// Cycle through the rows of the Excel file, pulling the data over to Notes

GoTo Records

print “Disconnecting from Excel…”

xlWorkbook.Close False ‘// Close the Excel file without saving (we made no changes)

Excel.Quit ‘// Close Excel

Set Excel = Nothing ‘// Free the memory

Records:

row = 1 ‘// Integers intialize to zero

written = 0

Do While True

Finish:

With xlSheet

row = row + 1

Set doc = db.createdocument ‘// Create a new doc

doc.Form = “frm_employeeDetails”

If .Cells( row, 1 ).Value=”" Then

Exit Sub

End If

doc.name = .Cells( row, 1 ).Value

doc.department = .Cells(row, 2 ).Value

doc.designation = .Cells(row, 3).value

doc.mobno = .Cells(row, 4).Value

Call doc.Save( True, True ) ‘// Save the new doc

‘GoTo Done

written = written + 1

Print Str(written)

If .Cells( row, 1 ).Value = “” Then

GoTo Done

End If

End With

Loop

Return

Done:

print “Import Complete – Total number of documents imported —> ” & written

End Sub
1. Create one button in Xpage and add the following code in the PostSaveDocument in the DataSource in the page which is used to execute the agent
var agen=database.getAgent(”[agentNae]“);
var uid=document2.getDocument().getNoteID();
agen.run(uid);

Html to Pdf using Java

The following Java Code is used to convert the HTML content into PDF

import java.io.*;

import java.io.FileOutputStream;
import org.xhtmlrenderer.pdf.ITextRenderer;
public class pdfCreation {

/**
* @Purpose : Converting html file into PDF.
* External jar file used iText-2.0.8.jar, core-renderer.jar
* xml-apis-xerces-2.9.1.jar
*/
public static void main(String[] args) {
try{
//html file path
String inputFile=”C:\\workspace\\html2Pdf\\src\\pack1\\sample.html”;
//Output pdf file path
String outputFP=”C:\\sample.pdf”;
File inputHTMLFile=new File(inputFile);
if(!inputHTMLFile.exists()){
System.out.println(”Input html File does not exists!”);
System exit(0);
}
String url=inputHTMLFile.toURI().toURL().toString();
OutputStream os=new FileOutputStream(outputFP);
ITextRenderer renderer=new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
System.out.println(”HTML file was converted into PDF”);
}
catch(Exception e){
e.printStackTrace();
}
}
}

Code to move the selected documents by enabling the folder in xpages


The following is the code which is used to move the selected documents by getting the selected document ID’s and moving it to the folder by enabling the new folder

var viewPanel=getComponent(”viewPanel1″);//get the componet of viewPanel

var docIDArray=viewPanel.getSelectedIds(); //get the array of document ids

var i;

database.enableFolder(”Temp1");

var projects:NotesView=database.getView(”Temp1");

var ndc:NotesDocumentCollection=projects.getAllEntries();

ndc.removeAllFromFolder(”Temp1");

if(docIDArray.length!=0)

{

for(i=0;i&ltdocIDArray.length;i++)

{

var docId=docIDArray[i];

var doc:NotesDocument=database.getDocumentByID(docId);

doc.putInFolder(”Temp1");

}

}


var viewPanel=getComponent(”viewPanel1");//get the componet of viewPanel


var docIDArray=viewPanel.getSelectedIds(); //get the array of document ids


var i;


database.enableFolder(”Temp1");


var projects:NotesView=database.getView(”view name“);


var ndc:NotesDocumentCollection=projects.getAllEntries();


ndc.removeAllFromFolder(”Temp1″);


if(docIDArray.length!=0)


{


for(i=0;i&ltdocIDArray.length;i++)


{


var docId=docIDArray[i];


var doc:NotesDocument=database.getDocumentByID(docId);


doc.putInFolder(”Temp1″);


}


}