Read Excel with Macro from Java - java

I have excel. and I create macro to the excel file to read data from other resources. the macro run every second and update its excel cells.
Now, I want to build java program to read the excel data every seconds to. I have try Apache POI, but after I check the documentation ti doesn't support reading excel file with macro.
I read from some resources Java Com Bridge (JCOB) can be used to read excel with macro. I've try, But the cell value still not updated every times I try my code.
import com.jacob.com.*;
import com.jacob.activeX.*;
public class ExcelTest {
private static ActiveXComponent xl;
private static Dispatch workbooks = null;
private static Dispatch workbook = null;
private static Dispatch sheet = null;
private static String filename = null;
private static boolean readonly = false;
public static void main(String[] args) {
String file = "D:\\tutorial\\ApachePoi\\ratesource.xls";
OpenExcel(file, false); // do not show false to open Excel
System.out.println(GetValue("B46"));
}
private static void OpenExcel(String file, boolean f) {
try {
filename = file;
xl = new ActiveXComponent("Excel.Application");
xl.setProperty("Visible", new Variant(f));
workbooks = xl.getProperty("Workbooks").toDispatch();
workbook = Dispatch.invoke(
workbooks,
"Open",
Dispatch.Method,
new Object[] { filename, new Variant(false),
new Variant(readonly) },// whether to open read-only
new int[1]).toDispatch();
} catch (Exception e) {
e.printStackTrace();
}
}
// Read value
private static String GetValue(String position) {
if (workbook == null) {
System.out.println("workbook is null");
}
sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
Object cell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[]{position}, new int[1]).toDispatch();
String value = Dispatch.get((Dispatch) cell, "Value").toString();
return value;
}
//1.3638356164383563
//1.3638356164383563
private static void SetValue (String position, String type, String value)
{
}
}

I am unfamiliar with an Excel engine capable of doing what you describe.
Have you considered talking to Excel instead and ask it for its values when running your spread-sheet? I believe you can do so with ODBC.
Another approach might be creating an OpenOffice version of your sheet and talk to OpenOffice instead.

One pitfall is poi don't change the value of the existing excel write to another file and you can see the difference
workbook.getSheet().getRow(1).getCell(0).setValue("test");
and write this(changed) workbook to another file
public void writeFile(String fileName) {
FileOutputStream fos;
try {
fos = new FileOutputStream(fileName);
getWorkbook().write(fos);
fos.close();
} catch (IOException e) {
System.out.println("IOException occurs");
e.printStackTrace();
}
}

Related

Value is not getting printed in Datasheet excel for setCellValue() using Selenium Apache POI Java webdriver

Value is not getting printed in Datasheet excel using Selenium Apache POI Java webdriver. Please help to print the value in the Datasheet excel.
Requirement is to pass sheet name, rownumber, Column number and text to be printed in the excel. By this code, text is not getting printed in Datasheet excel.
class ReadDataSheet:
public class ReadDataSheet {
public WebDriver dr;
public HSSFWorkbook wb;
public HSSFSheet ws;
public int rowCount;
public String className;
public String sheetName;
public String colName;
public String value;
public String setValue(String SheetName, String rowName, String columnHeader, String textToPrint) {
try {
FileInputStream file = new FileInputStream(new File("./DataSheet.xls"));
wb = new HSSFWorkbook(file);
ws = wb.getSheet(SheetName);
int rownumber = getRownumber(rowName, columnHeader);
int columnNumber = getColumnNumber(columnHeader);
Cell cell;
cell = ws.getRow(rownumber).createCell(columnNumber);
cell.setCellValue(textToPrint);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
Class Login:
public class Login{
public ReadDataSheet rds = new ReadDataSheet();
public void printValue() throws Exception {
rds.setValue("data details","Test case 1","Page Title","Selenium")
}
}
Class TC_01:
public class TC_01 extends Login
{
#Test
public void page() throws Exception
{
Login login = new Login(remoteDriver);
login.printValue();
}
}
To write value to excel, you need to save the excel sheet. Using folloing snippet at the end of
setValue method:
FileOutputStream fileOut = new FileOutputStream("poi-generated-file.xlsx");
workbook.write(fileOut);
fileOut.close();
// Closing the workbook
workbook.close();
Please share more details if it is not the problem you are facing.
The error seems to be in the getRownumber(rowName, columnHeader); method.
Is there any particular reason why this method requires columnHeader as a parameter?
It should only take rowName as input.
Can you please publish the code for these two methods (getRownumber and getColumnNumber) as well as upload the data sheet format?

I want to write the ouput of displayDirectoryContents to a excel sheet

I want to write the output of the displayDirectoryContents to a excel sheet
I have tried using the Apache POI method I want to get the output to a excel sheet
Folder and filename in one column and
the name of the files in another column
import statements
public class Excel {
private static String dest = "C:\\Users\\mahselva\\testexcel.xls";
private static HSSFWorkbook myWorkBook = new HSSFWorkbook();
private static HSSFSheet mySheet = myWorkBook.createSheet();
public static void excelLog(String filename, String message, int rowNum)
{
HSSFRow myRow = null;
HSSFCell myCell = null;
String excelData[][] = new String[1][2];
excelData[0][0] = filename;
excelData[0][1] = message;
myRow = mySheet.createRow(rowNum);
for (int cellNum = 0; cellNum < 2; cellNum++) {
myCell = myRow.createCell(cellNum);
myCell.setCellValue(excelData[0][cellNum]);
}
try {
FileOutputStream out = new FileOutputStream(dest);
myWorkBook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
File currentDir = new File("C:\\OracleATS\\openScript"); // current
directory
displayDirectoryContents(currentDir);
}
public static void displayDirectoryContents(File dir) {
try {
int i = 0;
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
Path path = Paths.get(file.getCanonicalPath());
//System.out.println("Folder"
+path.getFileName().toString());
excelLog("Folder",path.getFileName().toString(),i);
i++;
displayDirectoryContents(file);
} else {
Path path = Paths.get(file.getCanonicalPath());
//System.out.println(path.getFileName().toString());
excelLog("File",path.getFileName().toString(),i);
i++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I want two columns in an excel sheet with column 1 containing File or
Folder and column 2 containing the name of the file/folder
eg
File books.xml
Folder Script
Thus i want to write the output to the excel sheet
i am using the function excel log to write to the output screen
I use this to write to an excel - however I make it .csv format, not xls. Therefore this might not be what you need, but it's still partially useful as it's writing in a file that can be opened by excel efortlessly.
public static void printAtFile(String filename, String header, String content[])
{
filename+=".csv";
System.out.println("Start creating file "+filename);
PrintWriter writer = null;
try {
writer = new PrintWriter(filename);
writer.println(header);
for(String u:content)
writer.println(u);
writer.close();
} catch (Exception ex) {
System.out.println("Error while writing at file "+filename);
}
}

Exporting automated value to excel file in java

I am using selenium webdriver in eclipse with Java for a project at work. I am in need of exporting a value to an excel file. I am able to write to an excel file but am unable to export the specific value to the excel file.
Here is the automation process for getting the value:
public void AbbeyNational (String AbbeyNationalURL, String AN_AccCookiesButton, String AN_MortgageTabButton, String AN_ExistingCustomerButton, String AN_FollowRateButton, String AN_SVRButton, String AN_RateFld)throws InterruptedException {
driver.get(AbbeyNationalURL);
driver.findElement(By.xpath(AN_AccCookiesButton)).click();
driver.findElement(By.linkText(AN_MortgageTabButton)).click();
driver.findElement(By.xpath(AN_ExistingCustomerButton)).click();
driver.findElement(By.xpath(AN_FollowRateButton)).click();
driver.findElement(By.xpath(AN_SVRButton)).click();
String a = driver.findElement(By.cssSelector(AN_RateFld)).getText();
String AN_Rate = a.substring(54,58);
System.out.println(AN_Rate);
}
The 'AN_Rate' variable holds the value after automation.
The value prints to the console but I need the value to be exported to the excel file with the use of automation. Can anyone help me with this?
Additionally, here is my code for writing to an excel file:
public void writeToExcel(String AN_Rate) throws IOException{
File file = new File(filePath+"\\"+fileName);
XSSFWorkbook IRWorkbook = new XSSFWorkbook();
XSSFSheet Sheet0 = IRWorkbook.createSheet();
Sheet0.createRow(0).createCell(0).setCellValue(AN_Rate);
try {
FileOutputStream fos = new FileOutputStream(file);
IRWorkbook.write(fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
you can use this code for large set of data :
public class ExcelWriter {
// LinkedHashMap to maintain the insertion order
public static Map<String, String> dataMap = new LinkedHashMap<>();
public static String filePath = "D:\\ECLIPSE-WORKSPACE\\playground\\src\\main\\resources";
public static String fileName = "demo-data";
public static void main(String[] args) throws IOException {
String AN_Rate = "Data-1";
String BN_Rate = "Data-2";
String CN_Rate = "Data-3";
dataMap.put("AN_Rate", AN_Rate);
dataMap.put("BN_Rate", BN_Rate);
dataMap.put("CN_Rate", CN_Rate);
writeToExcel(dataMap, filePath, fileName);
}
public static void writeToExcel(Map<String, String> dataMap, String filePath, String fileName) throws IOException {
File file = new File(filePath + "\\" + fileName + ".xlsx");
if (file.exists()) {
file.delete();
}
file.createNewFile();
XSSFWorkbook IRWorkbook = new XSSFWorkbook();
XSSFSheet sheet = IRWorkbook.createSheet();
List<String> headers = dataMap.keySet().stream().collect(Collectors.toList()); // all key values in a list
List<String> data = new ArrayList<>(dataMap.values()); // all data in a list
setHeadersAndFillData(sheet, headers, data); // filling excel sheet with headers and corresponding data
try {
FileOutputStream fos = new FileOutputStream(file);
IRWorkbook.write(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void setHeadersAndFillData(XSSFSheet sheet, List<String> headers, List<String> data) {
int headersSize = headers.size();
int dataSize = headers.size();
Row headerRow = sheet.createRow(0);
Row dataRow = sheet.createRow(1);
setCells(headers, headersSize, headerRow);
setCells(data, dataSize, dataRow);
}
private static void setCells(List<String> cellData, int headersSize, Row row) {
for (int rn = 0; rn < headersSize; rn++) {
row.createCell(rn).setCellValue(cellData.get(rn));
}
}
}
Just edit your method writeToExcel with :
Replace :File file = new File(filePath+"\\"+fileName);
with added file extention
File file = new File(filePath+"\\"+fileName + ".xlsx");
and than the value will be exported to excel file.
And if there is large set of data than you should store all values in a static map and than export all values to excel at once.

how to send an excel file with servlet

im trying to create an excel file on a servlet and send it to the client browser when i did it on a stand alone program the file was created on my computer but when i tried to do it on a servlet it did nothing
servlet:
response.setContentType("text/html;charset=UTF-8");
// PrintWriter out = response.getWriter();
String[] items=request.getParameterValues("lecture");
String course=request.getParameter("course");
int sheets=Integer.parseInt(request.getParameter("sheets"));
List <XlElement> xlElements=getAllElements(items);
ServletOutputStream output=response.getOutputStream();
try
{
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename="+course+".xls");
CreateXl xl=new CreateXl();
xl.createScadualFile(output, xlElements, sheets);
output.println(course);
}
catch (Exception e)
{
System.out.println(e.toString());
throw new ServletException("Exception in Excel Sample Servlet", e);
}
output.close();
createXl class
private List<WritableSheet> xlSheets;
private String[] days={"א","ב","ג","ד","ה"};
private final int numOfClasses=9;
private final int cellHeight= 1020;
private final int cellWidth=15;
public void createScadualFile(ServletOutputStream output, List <XlElement> items,int sheets) throws IOException, WriteException{
xlSheets=new ArrayList<WritableSheet>();
WritableWorkbook workbook = Workbook.createWorkbook(output);
for(int i=0;i<sheets;i++){
WritableSheet sheet = workbook.createSheet("week "+(i+1), i);
xlSheets.add(sheet);
}
for(WritableSheet s: xlSheets){
initSheet(s);
}
for(XlElement e: items){
insertElement(e);
}
workbook.write();
workbook.close();
}
private WritableCellFormat getCellFormat(Colour colour, Pattern pattern) throws WriteException {
WritableFont cellFont = new WritableFont(WritableFont.TIMES, 12);
WritableCellFormat cellFormat = new WritableCellFormat(cellFont);
cellFormat.setBackground(colour, pattern);
cellFormat.setWrap(true);
cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.TOP);
return cellFormat;
}
private void initSheet(WritableSheet s) throws WriteException{
for(int i=0;i<days.length;i++){
Label l=new Label(i+1,0,days[i],getCellFormat(Colour.GREY_25_PERCENT,Pattern.SOLID));
s.setColumnView(i+1,cellWidth );
s.addCell(l);
}
for(int i=0;i<numOfClasses;i++){
Label l=new Label(0,i+1,Integer.toString(i+1),getCellFormat(Colour.GREY_25_PERCENT,Pattern.SOLID));
s.setRowView(i+1, cellHeight);
s.addCell(l);
}
}
private void insertElement(XlElement e) throws WriteException{
Label l=new Label(e.getCol(),e.getRow(),e.toXlString(), getCellFormat(Colour.RED,Pattern.SOLID));
xlSheets.get(e.getWeek()).mergeCells(e.getCol(), e.getRow(), e.getCol(), e.getRow()+e.getSpan()-1);
xlSheets.get(e.getWeek()).addCell(l);
}
dose anybody know what I am doing wrong?
First, you should only call response.setContentType() once. You want to return an Excel, so take out the one where you are setting the content type to "text/html;charset=UTF-8".
Second, writing text to the output stream after writing the binary file to it will screw it up. Take out the output.println(course);
Third, I really don't think the output.close(); is needed either, so you might try taking that out as well.

Error While Reading Large Excel Files (xlsx) Via Apache POI

I am trying to read large excel files xlsx via Apache POI, say 40-50 MB. I am getting out of memory exception. The current heap memory is 3GB.
I can read smaller excel files without any issues. I need a way to read large excel files and then them back as response via Spring excel view.
public class FetchExcel extends AbstractView {
#Override
protected void renderMergedOutputModel(
Map model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
String fileName = "SomeExcel.xlsx";
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
OPCPackage pkg = OPCPackage.open("/someDir/SomeExcel.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(pkg);
ServletOutputStream respOut = response.getOutputStream();
pkg.close();
workbook.write(respOut);
respOut.flush();
workbook = null;
response.setHeader("Content-disposition", "attachment;filename=\"" +fileName+ "\"");
}
}
I first started off using XSSFWorkbook workbook = new XSSFWorkbook(FileInputStream in);
but that was costly per Apache POI API, so I switched to OPC package way but still the same effect. I don't need to parse or process the file, just read it and return it.
Here is an example to read a large xls file using sax parser.
public void parseExcel(File file) throws IOException {
OPCPackage container;
try {
container = OPCPackage.open(file.getAbsolutePath());
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(container);
XSSFReader xssfReader = new XSSFReader(container);
StylesTable styles = xssfReader.getStylesTable();
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
while (iter.hasNext()) {
InputStream stream = iter.next();
processSheet(styles, strings, stream);
stream.close();
}
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (OpenXML4JException e) {
e.printStackTrace();
}
}
protected void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, InputStream sheetInputStream) throws IOException, SAXException {
InputSource sheetSource = new InputSource(sheetInputStream);
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = saxFactory.newSAXParser();
XMLReader sheetParser = saxParser.getXMLReader();
ContentHandler handler = new XSSFSheetXMLHandler(styles, strings, new SheetContentsHandler() {
#Override
public void startRow(int rowNum) {
}
#Override
public void endRow() {
}
#Override
public void cell(String cellReference, String formattedValue) {
}
#Override
public void headerFooter(String text, boolean isHeader, String tagName) {
}
},
false//means result instead of formula
);
sheetParser.setContentHandler(handler);
sheetParser.parse(sheetSource);
} catch (ParserConfigurationException e) {
throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());
}
You don't mention whether you need to modify the spreadsheet or not.
This may be obvious, but if you don't need to modify the spreadsheet, then you don't need to parse it and write it back out, you can simply read bytes from the file, and write out bytes, as you would with, say an image, or any other binary format.
If you do need to modify the spreadsheet before sending it to the user, then to my knowledge, you may have to take a different approach.
Every library that I'm aware of for reading Excel files in Java reads the whole spreadsheet into memory, so you'd have to have 50MB of memory available for every spreadsheet that could possibly be concurrently processed. This involves, as others have pointed out, adjusting the heap available to the VM.
If you need to process a large number of spreadsheets concurrently, and can't allocate enough memory, consider using a format that can be streamed, instead of read all at once into memory. CSV format can be opened by Excel, and I've had good results in the past by setting the content-type to application/vnd.ms-excel, setting the attachment filename to something ending in ".xls", but actually returning CSV content. I haven't tried this in a couple of years, so YMMV.
In the bellwo example I'll add a complete code how to parse a complete excel file (for me 60Mo) into list of object without any problem of "out of memory" and work fine:
import java.util.ArrayList;
import java.util.List;
class DistinctByProperty {
private static OPCPackage xlsxPackage = null;
private static PrintStream output= System.out;
private static List<MassUpdateMonitoringRow> resultMapping = new ArrayList<>();
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\aberguig032018\\Downloads\\your_excel.xlsx");
double bytes = file.length();
double kilobytes = (bytes / 1024);
double megabytes = (kilobytes / 1024);
System.out.println("Size "+megabytes);
parseExcel(file);
}
public static void parseExcel(File file) throws IOException {
try {
xlsxPackage = OPCPackage.open(file.getAbsolutePath(), PackageAccess.READ);
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(xlsxPackage);
XSSFReader xssfReader = new XSSFReader(xlsxPackage);
StylesTable styles = xssfReader.getStylesTable();
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
int index = 0;
while (iter.hasNext()) {
try (InputStream stream = iter.next()) {
String sheetName = iter.getSheetName();
output.println();
output.println(sheetName + " [index=" + index + "]:");
processSheet(styles, strings, new MappingFromXml(resultMapping), stream);
}
++index;
}
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (OpenXML4JException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
private static void processSheet(StylesTable styles, ReadOnlySharedStringsTable strings, MappingFromXml mappingFromXml, InputStream sheetInputStream) throws IOException, SAXException {
DataFormatter formatter = new DataFormatter();
InputSource sheetSource = new InputSource(sheetInputStream);
try {
XMLReader sheetParser = SAXHelper.newXMLReader();
ContentHandler handler = new XSSFSheetXMLHandler(
styles, null, strings, mappingFromXml, formatter, false);
sheetParser.setContentHandler(handler);
sheetParser.parse(sheetSource);
System.out.println("Size of Array "+resultMapping.size());
} catch(ParserConfigurationException e) {
throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());
}
}
}
you have to add a calss that implements
SheetContentsHandler
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
public class MappingFromXml implements SheetContentsHandler {
private List<myObject> result = new ArrayList<>();
private myObject myObject = null;
private int lineNumber = 0;
/**
* Number of columns to read starting with leftmost
*/
private int minColumns = 25;
/**
* Destination for data
*/
private PrintStream output = System.out;
public MappingFromXml(List<myObject> list) {
this.result = list;
}
#Override
public void startRow(int i) {
output.println("iii " + i);
lineNumber = i;
myObject = new myObject();
}
#Override
public void endRow(int i) {
output.println("jjj " + i);
result.add(myObject);
myObject = null;
}
#Override
public void cell(String cellReference, String formattedValue, XSSFComment comment) {
int columnIndex = (new CellReference(cellReference)).getCol();
if(lineNumber > 0){
switch (columnIndex) {
case 0: {//Tech id
if (formattedValue != null && !formattedValue.isEmpty())
myObject.setId(Integer.parseInt(formattedValue));
}
break;
//TODO add other cell
}
}
}
#Override
public void headerFooter(String s, boolean b, String s1) {
}
}
For more information visite this link
I too faced the same issue of OOM while parsing xlsx file...after two days of struggle, I finally found out the below code that was really perfect;
This code is based on sjxlsx. It reads the xlsx and stores in a HSSF sheet.
[code=java]
// read the xlsx file
SimpleXLSXWorkbook = new SimpleXLSXWorkbook(new File("C:/test.xlsx"));
HSSFWorkbook hsfWorkbook = new HSSFWorkbook();
org.apache.poi.ss.usermodel.Sheet hsfSheet = hsfWorkbook.createSheet();
Sheet sheetToRead = workbook.getSheet(0, false);
SheetRowReader reader = sheetToRead.newReader();
Cell[] row;
int rowPos = 0;
while ((row = reader.readRow()) != null) {
org.apache.poi.ss.usermodel.Row hfsRow = hsfSheet.createRow(rowPos);
int cellPos = 0;
for (Cell cell : row) {
if(cell != null){
org.apache.poi.ss.usermodel.Cell hfsCell = hfsRow.createCell(cellPos);
hfsCell.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
hfsCell.setCellValue(cell.getValue());
}
cellPos++;
}
rowPos++;
}
return hsfSheet;[/code]

Categories