02-17-2010 04:09 PM
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.xml.rpc.ServiceException;
import org.alfresco.webservice.content.Content;
import org.alfresco.webservice.content.ContentServiceSoapBindingStub;
import org.alfresco.webservice.repository.QueryResult;
import org.alfresco.webservice.repository.RepositoryFault;
import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.Predicate;
import org.alfresco.webservice.types.Query;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.types.ResultSet;
import org.alfresco.webservice.types.ResultSetRow;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.WebServiceFactory;
import org.alfresco.webservice.util.ContentUtils;
public class ExportAction {
private String[] cols = { "name", "description", "title", "author" };
public static void main(String args[]) throws Exception {
// Start the session
try {
WebServiceFactory
.setEndpointAddress("http://localhost:8081/alfresco/api");
AuthenticationUtils.startSession("admin", "admin");
// executeSearch();
PrintWriter pw = new PrintWriter(
new FileWriter("C:\\temp\\exp.csv"));
CSVWriter csv = new CSVWriter(pw, false, ',', System
.getProperty("line.separator"));
ExportAction exp = new ExportAction();
String[] files = { "a_file2.txt", "a_file3.txt" };
// Call the search and update into the file pass csv object.
csv.writeln(exp.cols);
//for(int i=0;i<files.length;i++){
exp.exportData(csv, "a_file2.txt");
//}
csv.close();
} finally {
// End the session
AuthenticationUtils.endSession();
}
}
protected static final Store STORE = new Store(Constants.WORKSPACE_STORE,
"SpacesStore");
private boolean exportData(CSVWriter csv, String files) {
try {
Reference parentReference = null;
// Get a reference to the repository web service
RepositoryServiceSoapBindingStub repositoryService = WebServiceFactory
.getRepositoryService();
// Create a query object, looking for all items with alfresco in the
// name of text
// Query query = new Query(Constants.QUERY_LANG_LUCENE,
// "name:'SampleContent.txt'");
final String customSo = "@cm\\:name:\"" + files + "\"";
Query query = new Query();
query.setLanguage(Constants.QUERY_LANG_LUCENE);
query.setStatement(customSo);
QueryResult queryResult = repositoryService.query(STORE, query,
false);
// Display the results
ResultSet resultSet = queryResult.getResultSet();
ResultSetRow[] rows = resultSet.getRows();
if (rows == null) {
System.out.println("No query results found.");
} else {
String[] vals=outputResultSet(rows);
System.out.println("values Size ::"+vals.length);
csv.writeln(vals);
}
} catch (Exception exp) {
exp.printStackTrace();
}
// csv.write("Bala");
return false;
}
public static String[] outputResultSet(ResultSetRow[] rows) {
String[] values = new String[4];
try
{
System.out.println("Rowz Length::"+rows.length);
if (rows != null) {
for (int x = 0; x < rows.length; x++) {
ResultSetRow row = rows[x];
NamedValue[] columns = row.getColumns();
System.out.println("columns :: " + columns.length);
for (int y = 0; y < columns.length; y++) {
String fieldName = row.getColumns()[y].getName();
// String
// fieldValue=fieldName.substring(fieldName.lastIndexOf("::")+1);
fieldName = fieldName.substring(fieldName.lastIndexOf("}") + 1);
System.out.println("fieldName :: " + fieldName);
if (fieldName.equals("name")) {
values[0] = row.getColumns(y).getValue();
} else if (fieldName.equals("description")) {
values[1] = row.getColumns(y).getValue();
} else if (fieldName.equals("title")) {
values[2] = row.getColumns(y).getValue();
} else if (fieldName.equals("author")) {
values[3] = row.getColumns(y).getValue();
}
System.out.println("fieldValue ::"
+ row.getColumns(y).getValue());
/*
* ( System.out.println("row " + x + ": " +
* row.getColumns(y).getName() + " = " + ""+
* row.getColumns().length + row.getColumns(y).getValue());
*/
}
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
return values;
}
}
import java.io.EOFException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
/**
* Writes CSV (Comma Separated Value) files.
*
* This format is mainly used my Microsoft Word and Excel.
* Fields are separated by commas, and enclosed in
* quotes if they contain commas or quotes.
* Embedded quotes are doubled.
* Embedded spaces do not normally require surrounding quotes.
* The last field on the line is not followed by a comma.
* Null fields are represented by two commas in a row.
*
* @author copyright © 2002 Roedy Green Canadian Mind Products
* Roedy posted this code on Newsgroups:comp.lang.java.programmer on 27th March 2002.
*
* Heinrich added some stuff like comment ability and linewise working.
*
*/
public class CSVWriter {
/**
* Constructor
*
* @param pw PrintWriter where fields will be written.
* @param forceQuotes
* true if you want all fields surrounded in quotes,
* whether or not they contain commas, quotes or spaces.
* @param separator
* field separator character, usually ',' in North America,
* ';' in Europe and sometimes '\t' for tab.
* @param lineSeparator
* gives the delimiter for the line; is per default set to
* the system property 'line.separator'
*/
public CSVWriter(PrintWriter pw, boolean forceQuotes, char separator, String lineSeparator) {
this.pw = pw;
this.forceQuotes = forceQuotes;
this.separator = separator;
this.comment = "# ";
this.lineSeparator = lineSeparator;
} // end of CSVWriter
public CSVWriter(Writer w, boolean forceQuotes, char separator, String lineSeparator) {
this(new PrintWriter(w),forceQuotes,separator,lineSeparator);
}
/**
* Constructor with default field separator ','.
*
* @param pw PrintWriter where fields will be written.
*/
public CSVWriter(PrintWriter pw) {
this.pw = pw;
this.forceQuotes = false;
this.separator = ',';
this.comment = "# ";
this.lineSeparator = System.getProperty("line.separator");
} // end of CSVWriter
public CSVWriter(Writer w) {
this(new PrintWriter(w));
}
/**
* Constructor with default field separator ','.
*
* @param pw PrintWriter where fields will be written.
* @param comment Character used to start a comment line
*/
public CSVWriter(PrintWriter pw, char comment) {
this.pw = pw;
this.forceQuotes = false;
this.separator = ',';
this.comment = String.valueOf(comment) + " ";
this.lineSeparator = System.getProperty("line.separator");
} // end of CSVWriter
public CSVWriter(Writer w, char comment) {
this(new PrintWriter(w),comment);
}
/**
* PrintWriter where CSV fields will be written.
*/
PrintWriter pw;
/**
* true if you want all fields surrounded in quotes,
* whether or not they contain commas, quotes or spaces.
*/
boolean forceQuotes;
/*
* field separator character, usually ',' in North America,
* ';' in Europe and sometimes '\t' for tab.
*/
char separator;
/**
* true if there has was a field previously written to
* this line, meaning there is a comma pending to
* be written.
*/
boolean wasPreviousField = false;
/**
* Character to start a comment line with. May be '#' for example.
*/
String comment;
/**
* Line separator.
*/
String lineSeparator;
/**
* Writes a single coment line to the file given by the text.
* This is the text leaded by the comment char + " ", given in the constructor.
* @param text contains the comment text.
*/
public void writeCommentln(String text) {
if (wasPreviousField) writeln(); // close open line since we need to start a new one for comment
pw.print(comment);
//wasPreviousField = false; // to prevent a comma after the comment sign
write(text);
writeln();
} // end of writeComentln
/**
* Writes a single value in a line suited by a newline to the file given by the token.
* @param token contains the value.
*/
public void writeln(String token) {
write(token);
writeln();
} // end of writeln
/**
* Writes a new line in the CVS output file to demark the end of record.
*/
public void writeln() {
/* don't bother to write last pending comma on the line */
wasPreviousField = false;
pw.print(lineSeparator);
} // end of writeln
/**
* Writes a single line of comma separated values from the array given by line.
* @param line containig an array of tokens.
*/
public void writeln(String[] line) {
for(int ii=0; ii < line.length; ii++) {
write(line[ii]);
} // end of for
writeln(); // write newLine
} // end of writeln
/**
* Write one csv field to the file, followed by a separator
* unless it is the last field on the line. Lead and trailing
* blanks will be removed.
*
* @param s The string to write. Any additional quotes or
* embedded quotes will be provided by write.
*/
public void write(String s) {
if ( wasPreviousField ) {
pw.print(separator);
}
if ( s == null ) {
pw.print("");
return;
} // end of if s == null
s = s.trim();
if ( s.indexOf('\"') >= 0 ) {
/* worst case, needs surrounding quotes and internal quotes doubled */
pw.print ('\"');
for ( int i=0; i<s.length(); i++ ) {
char c = s.charAt(i);
if ( c == '\"' ) {
pw.print("\"\"");
} else {
pw.print©;
}
}
pw.print ('\"');
// end of if \"
} else if ( s.indexOf('\n') >=0 ) {
// bad case as well: having a new line in the token: \n
pw.print ('\"');
for ( int i=0; i<s.length(); i++ ) {
char c = s.charAt(i);
if ( c == '\n' ) {
pw.print("\\n");
} else {
pw.print©;
}
}
pw.print ('\"');
// end of if \n
} else if ( forceQuotes || s.indexOf(separator) >= 0 ) {
/* need surrounding quotes */
pw.print ('\"');
pw.print(s);
pw.print ('\"');
} else {
/* ordinary case, no surrounding quotes needed */
pw.print(s);
}
/* make a note to print trailing comma later */
wasPreviousField = true;
} // end of write
/**
* Close the PrintWriter.
*/
public void close() {
if ( pw != null ) {
pw.close();
pw = null;
} // end of if
} // end of close
/**
* Test driver
*
* @param args [0]: The name of the file.
*/
static public void main(String[] args) {
try {
// write out a test file
PrintWriter pw = new PrintWriter( new FileWriter("C:\\temp\\exp.csv"));
CSVWriter csv = new CSVWriter(pw, false, ',', System.getProperty("line.separator") );
// csv.writeCommentln("This is a test csv-file: '" + "C:\\temp\\exp.csv" + "'");
csv.write("abc");
csv.write("def");
csv.write("g h i");
csv.write("jk,l");
csv.write("m\"n\'o ");
csv.writeln();
csv.write("m\"n\'o ");
csv.write(" ");
csv.write("a");
csv.write("x,y,z");
csv.write("x;y;z");
csv.writeln();
csv.writeln(new String[] {"This", "is", "an", "array."});
csv.close();
} catch ( IOException e ) {
e.printStackTrace();
System.out.println(e.getMessage());
}
} // end main
} // end CSVWriter
// end of file
02-18-2010 12:39 AM
02-18-2010 03:19 AM
02-18-2010 08:09 PM
Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
String ROOT = "/app:company_home/cm:"+ISO9075.encode("Folder Name");
String path = "PATH:\"/" + ROOT + "//.\" AND TYPE:\"{http://www.alfresco.org/model/content/1.0}content\"";
final List<String> result = new ArrayList<String>();
final Query query = new Query(Constants.QUERY_LANG_LUCENE, path);
Node [] nodes = null;
try {
nodes = WebServiceFactory.getRepositoryService().get(new Predicate(null,STORE,query));
} catch (RepositoryFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
if (null != nodes){
NamedValue[] props = null;
for (final Node node : nodes) {
props = node.getProperties();
for(NamedValue prop : props) {
if(prop.getName().endsWith(Constants.PROP_NAME) == true) {
result.add(prop.getValue());
}
}
}
06-26-2014 06:30 AM
07-15-2017 04:20 AM
There is one generic example using which you can export any kind of data in excel.Below link will help you understanding that stuff.