Below is the java program to create and write a excel file.
ShibExcel.java
package mail2tickets;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ShibExcel {
public static WritableCellFormat timesBoldUnderline;
public static WritableCellFormat times;
private String inputFile;
public void setOutputFile(String inputFile) {
this.inputFile = inputFile;
}
public void createLabel(WritableSheet sheet)
throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format
times = new WritableCellFormat(times10pt);
// Lets automatically wrap the cells
times.setWrap(true);
// Create create a bold font with unterlines
WritableFont times10ptBoldUnderline = new WritableFont(
WritableFont.TIMES, 10, WritableFont.BOLD, false,
UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true);
CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
cv.setAutosize(true);
// Write a few headers
addCaption(sheet, 0, 0, "Sender_name");
addCaption(sheet, 1, 0, "Sender_email");
addCaption(sheet, 2, 0, "Subject");
addCaption(sheet, 3, 0, "Email_to");
addCaption(sheet, 4, 0, "Email_cc");
addCaption(sheet, 5, 0, "Email_bcc");
addCaption(sheet, 6, 0, "create_time");
addCaption(sheet, 7, 0, "submit_time");
addCaption(sheet, 8, 0, "isRead");
addCaption(sheet, 9, 0, "hasAttachment");
addCaption(sheet, 10, 0, "hasForwarded");
addCaption(sheet, 11, 0, "hasReplied");
addCaption(sheet, 12, 0, "Importance");
}
public void addCaption(WritableSheet sheet, int column, int row, String s)
throws RowsExceededException, WriteException {
Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
}
public void addNumber(WritableSheet sheet, int column, int row,
Integer integer) throws WriteException, RowsExceededException {
Number number;
number = new Number(column, row, integer, times);
sheet.addCell(number);
}
public void addLabel(WritableSheet sheet, int column, int row, String s)
throws WriteException, RowsExceededException {
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);
}
}
The below program reads a .pst file and save the data in excel file
InboxMail.java
package mail2tickets;
import java.io.*;
import Shibsentmail.com.pff.*;
import java.util.*;
import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class InboxMail {
WritableCellFormat timesBoldUnderline;
WritableCellFormat times;
public static void main(String[] args) {
new InboxMail("D:/Data/Outlook Emails/Personal Folders.pst","D:/");
}
private InboxMail(String string) {
//throw new UnsupportedOperationException("Not yet implemented");
}
public void Smail(String file,String location)
{
new InboxMail(file.replace("\\", "/"),location.replace("\\", "/"));
}
public InboxMail(String filename,String location) {
try {
PSTFile pstFile = new PSTFile(filename);
System.out.println(pstFile.getMessageStore().getDisplayName());
ShibExcel WE = new ShibExcel();
String inputFile1 = location+"/ShibInbox.xls";
System.out.println(inputFile1);
WE.setOutputFile(inputFile1);
File file = new File(inputFile1);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelsheet1 = workbook.getSheet(0);
WE.createLabel(excelsheet1);
processFolder(pstFile.getRootFolder(), excelsheet1,location);
workbook.write();
workbook.close();
} catch (Exception err) {
err.printStackTrace();
}
}
int depth = -1;
int inc = 1;
public void processFolder(PSTFolder folder, WritableSheet excelsheet,String location)
throws PSTException, java.io.IOException, WriteException, RowsExceededException {
try {
File f = new File(location+"/InboxMail_Log.log");
Boolean b = f.createNewFile();
System.out.println(b);
BufferedWriter outt = new BufferedWriter(new FileWriter(f, true));
depth++;
// the root folder doesn't have a display name
if (depth > 0) {
if (folder.getDisplayName().equalsIgnoreCase("Inbox")) {
//printDepth();
// System.out.println(folder.getDisplayName());
outt.write(folder.getDisplayName() + " \\n");
}
}
// go through the folders...
if (folder.hasSubfolders()) {
Vector<PSTFolder> childFolders = folder.getSubFolders();
for (PSTFolder childFolder : childFolders) {
processFolder(childFolder, excelsheet,location);
}
}
// and now the emails for this folder
if ((folder.getContentCount() > 0) && (folder.getDisplayName().equalsIgnoreCase("Inbox"))) {
depth++;
PSTMessage email = (PSTMessage) folder.getNextChild();
while (email != null) {
// WritableSheet sheet1 = null;
try {
outt.write("|- Sender Name : " + email.getSenderName() + " \\n");
new ShibExcel().addLabel(excelsheet, 0, inc, email.getSenderName().toString());
outt.write("|- Sender Email : " + email.getSenderEmailAddress() + " \\n");
new ShibExcel().addLabel(excelsheet, 1, inc, email.getSenderEmailAddress());
outt.write("|- Email Subject : " + email.getSubject() + " \\n");
new ShibExcel().addLabel(excelsheet, 2, inc, email.getSubject());
outt.write("|- Display to : " + email.getDisplayTo().replace("'", "") + " \\n");
new ShibExcel().addLabel(excelsheet, 3, inc, email.getDisplayTo().replace("'", ""));
outt.write("|- CC : " + email.getDisplayCC().replace("'", "") + " \\n");
new ShibExcel().addLabel(excelsheet, 4, inc, email.getDisplayCC().replace("'", ""));
outt.write("|- BCC : " + email.getDisplayBCC().replace("'", "") + " \\n");
new ShibExcel().addLabel(excelsheet, 5, inc, email.getDisplayBCC().replace("'", ""));
outt.write("|- Creation Time : " + email.getCreationTime() + " \\n");
new ShibExcel().addLabel(excelsheet, 6, inc, email.getCreationTime().toLocaleString());
outt.write("|- Submit Time : " + email.getClientSubmitTime() + " \\n");
new ShibExcel().addLabel(excelsheet, 7, inc, email.getClientSubmitTime().toString());
outt.write("|- Read : " + email.isRead() + " \\n");
new ShibExcel().addLabel(excelsheet, 8, inc, email.isRead() + "");
outt.write("|- Attachments : " + email.hasAttachments() + " \\n");
new ShibExcel().addLabel(excelsheet, 9, inc, email.hasAttachments() + "");
outt.write("|- Forwarded : " + email.hasForwarded() + " \\n");
new ShibExcel().addLabel(excelsheet, 10, inc, email.hasForwarded() + "");
outt.write("|- Replied : " + email.hasReplied() + " \\n");
new ShibExcel().addLabel(excelsheet, 11, inc, email.hasReplied() + "");
outt.write("|- Importance : " + email.getImportance() + " \\n \\n");
new ShibExcel().addLabel(excelsheet, 12, inc, email.getImportance() + "");
System.out.println(inc);
} catch (Exception sqle1) {
System.out.println(sqle1);
}
email = (PSTMessage) folder.getNextChild();
inc++;
}
depth--;
}
depth--;
outt.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
public void printDepth() {
for (int x = 0; x < depth - 1; x++) {
System.out.print(" | ");
}
System.out.print(" |- ");
}
}
ShibExcel.java
package mail2tickets;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ShibExcel {
public static WritableCellFormat timesBoldUnderline;
public static WritableCellFormat times;
private String inputFile;
public void setOutputFile(String inputFile) {
this.inputFile = inputFile;
}
public void createLabel(WritableSheet sheet)
throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format
times = new WritableCellFormat(times10pt);
// Lets automatically wrap the cells
times.setWrap(true);
// Create create a bold font with unterlines
WritableFont times10ptBoldUnderline = new WritableFont(
WritableFont.TIMES, 10, WritableFont.BOLD, false,
UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true);
CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
cv.setAutosize(true);
// Write a few headers
addCaption(sheet, 0, 0, "Sender_name");
addCaption(sheet, 1, 0, "Sender_email");
addCaption(sheet, 2, 0, "Subject");
addCaption(sheet, 3, 0, "Email_to");
addCaption(sheet, 4, 0, "Email_cc");
addCaption(sheet, 5, 0, "Email_bcc");
addCaption(sheet, 6, 0, "create_time");
addCaption(sheet, 7, 0, "submit_time");
addCaption(sheet, 8, 0, "isRead");
addCaption(sheet, 9, 0, "hasAttachment");
addCaption(sheet, 10, 0, "hasForwarded");
addCaption(sheet, 11, 0, "hasReplied");
addCaption(sheet, 12, 0, "Importance");
}
public void addCaption(WritableSheet sheet, int column, int row, String s)
throws RowsExceededException, WriteException {
Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
}
public void addNumber(WritableSheet sheet, int column, int row,
Integer integer) throws WriteException, RowsExceededException {
Number number;
number = new Number(column, row, integer, times);
sheet.addCell(number);
}
public void addLabel(WritableSheet sheet, int column, int row, String s)
throws WriteException, RowsExceededException {
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);
}
}
The below program reads a .pst file and save the data in excel file
InboxMail.java
package mail2tickets;
import java.io.*;
import Shibsentmail.com.pff.*;
import java.util.*;
import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class InboxMail {
WritableCellFormat timesBoldUnderline;
WritableCellFormat times;
public static void main(String[] args) {
new InboxMail("D:/Data/Outlook Emails/Personal Folders.pst","D:/");
}
private InboxMail(String string) {
//throw new UnsupportedOperationException("Not yet implemented");
}
public void Smail(String file,String location)
{
new InboxMail(file.replace("\\", "/"),location.replace("\\", "/"));
}
public InboxMail(String filename,String location) {
try {
PSTFile pstFile = new PSTFile(filename);
System.out.println(pstFile.getMessageStore().getDisplayName());
ShibExcel WE = new ShibExcel();
String inputFile1 = location+"/ShibInbox.xls";
System.out.println(inputFile1);
WE.setOutputFile(inputFile1);
File file = new File(inputFile1);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelsheet1 = workbook.getSheet(0);
WE.createLabel(excelsheet1);
processFolder(pstFile.getRootFolder(), excelsheet1,location);
workbook.write();
workbook.close();
} catch (Exception err) {
err.printStackTrace();
}
}
int depth = -1;
int inc = 1;
public void processFolder(PSTFolder folder, WritableSheet excelsheet,String location)
throws PSTException, java.io.IOException, WriteException, RowsExceededException {
try {
File f = new File(location+"/InboxMail_Log.log");
Boolean b = f.createNewFile();
System.out.println(b);
BufferedWriter outt = new BufferedWriter(new FileWriter(f, true));
depth++;
// the root folder doesn't have a display name
if (depth > 0) {
if (folder.getDisplayName().equalsIgnoreCase("Inbox")) {
//printDepth();
// System.out.println(folder.getDisplayName());
outt.write(folder.getDisplayName() + " \\n");
}
}
// go through the folders...
if (folder.hasSubfolders()) {
Vector<PSTFolder> childFolders = folder.getSubFolders();
for (PSTFolder childFolder : childFolders) {
processFolder(childFolder, excelsheet,location);
}
}
// and now the emails for this folder
if ((folder.getContentCount() > 0) && (folder.getDisplayName().equalsIgnoreCase("Inbox"))) {
depth++;
PSTMessage email = (PSTMessage) folder.getNextChild();
while (email != null) {
// WritableSheet sheet1 = null;
try {
outt.write("|- Sender Name : " + email.getSenderName() + " \\n");
new ShibExcel().addLabel(excelsheet, 0, inc, email.getSenderName().toString());
outt.write("|- Sender Email : " + email.getSenderEmailAddress() + " \\n");
new ShibExcel().addLabel(excelsheet, 1, inc, email.getSenderEmailAddress());
outt.write("|- Email Subject : " + email.getSubject() + " \\n");
new ShibExcel().addLabel(excelsheet, 2, inc, email.getSubject());
outt.write("|- Display to : " + email.getDisplayTo().replace("'", "") + " \\n");
new ShibExcel().addLabel(excelsheet, 3, inc, email.getDisplayTo().replace("'", ""));
outt.write("|- CC : " + email.getDisplayCC().replace("'", "") + " \\n");
new ShibExcel().addLabel(excelsheet, 4, inc, email.getDisplayCC().replace("'", ""));
outt.write("|- BCC : " + email.getDisplayBCC().replace("'", "") + " \\n");
new ShibExcel().addLabel(excelsheet, 5, inc, email.getDisplayBCC().replace("'", ""));
outt.write("|- Creation Time : " + email.getCreationTime() + " \\n");
new ShibExcel().addLabel(excelsheet, 6, inc, email.getCreationTime().toLocaleString());
outt.write("|- Submit Time : " + email.getClientSubmitTime() + " \\n");
new ShibExcel().addLabel(excelsheet, 7, inc, email.getClientSubmitTime().toString());
outt.write("|- Read : " + email.isRead() + " \\n");
new ShibExcel().addLabel(excelsheet, 8, inc, email.isRead() + "");
outt.write("|- Attachments : " + email.hasAttachments() + " \\n");
new ShibExcel().addLabel(excelsheet, 9, inc, email.hasAttachments() + "");
outt.write("|- Forwarded : " + email.hasForwarded() + " \\n");
new ShibExcel().addLabel(excelsheet, 10, inc, email.hasForwarded() + "");
outt.write("|- Replied : " + email.hasReplied() + " \\n");
new ShibExcel().addLabel(excelsheet, 11, inc, email.hasReplied() + "");
outt.write("|- Importance : " + email.getImportance() + " \\n \\n");
new ShibExcel().addLabel(excelsheet, 12, inc, email.getImportance() + "");
System.out.println(inc);
} catch (Exception sqle1) {
System.out.println(sqle1);
}
email = (PSTMessage) folder.getNextChild();
inc++;
}
depth--;
}
depth--;
outt.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
public void printDepth() {
for (int x = 0; x < depth - 1; x++) {
System.out.print(" | ");
}
System.out.print(" |- ");
}
}
No comments:
Post a Comment