<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Excel - Java Archives | Selenium Webdriver Appium Complete Tutorial</title>
	<atom:link href="https://automationtesting.in/category/excel-java/feed/" rel="self" type="application/rss+xml" />
	<link>https://automationtesting.in/category/excel-java/</link>
	<description>Automation Testing</description>
	<lastBuildDate>Wed, 15 Mar 2017 03:20:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2.2</generator>
	<item>
		<title>Row Count And Column Count in Excel</title>
		<link>https://automationtesting.in/row-count-and-column-count-in-excel/</link>
					<comments>https://automationtesting.in/row-count-and-column-count-in-excel/#comments</comments>
		
		<dc:creator><![CDATA[Krishna Sakinala]]></dc:creator>
		<pubDate>Fri, 06 Jan 2017 03:39:30 +0000</pubDate>
				<category><![CDATA[Excel - Java]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[column count]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[excel with java]]></category>
		<category><![CDATA[excel with selenium]]></category>
		<category><![CDATA[how to get column count]]></category>
		<category><![CDATA[how to get column count in excel]]></category>
		<category><![CDATA[how to get row count]]></category>
		<category><![CDATA[how to get row count in excel]]></category>
		<category><![CDATA[micorsoft excel]]></category>
		<category><![CDATA[row]]></category>
		<category><![CDATA[row count]]></category>
		<category><![CDATA[Selenium]]></category>
		<guid isPermaLink="false">http://automationtesting.in/?p=1179</guid>

					<description><![CDATA[<p>Row count and column count in excel will discuss how we can get the information about row count and column count of an excel while automating any application using selenium web driver. Usually we will use the excel sheet to maintain the test data. We might get doubt that why we need to put the [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://automationtesting.in/row-count-and-column-count-in-excel/">Row Count And Column Count in Excel</a> appeared first on <a rel="nofollow" href="https://automationtesting.in">Selenium Webdriver Appium Complete Tutorial</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
Row count and column count in excel will discuss how we can get the information about row count and column count of an excel while automating any application using selenium web driver. Usually we will use the excel sheet to maintain the test data. We might get doubt that why we need to put the test data/results in excel. If an application need so much of data to be feed to test; then we will manage those data using external sources like text file, xml file and excel file etc… . Among these; Excel is very user friendly that we can organize the data very easily and we can read and write the data how we want. So, we mostly use excel to maintain the test data and results. In order to create a data driven framework then it is easy with using excel.</p>
<div class="post-video"><iframe loading="lazy" width="1165" height="874" src="https://www.youtube.com/embed/gjsXCigVMXU?wmode=transparent&#038;fs=1"  allowfullscreen></iframe></div>
<p>So, if you want to read huge number of test data from the excel sheet to feed to the application then we need to know how many rows and columns are there in the excel. If you know exact information about the test data then only we can get the information from the excel in proper way. For this we need to use some third party API which are available. In those one of the famous is <strong>Apache POI</strong>. In this blog we will use Apache POI to  get the row count and column count from the excel sheet. To interact with the excel, Apache POI given so many classes to interact with the excel and read and write the data. To read/write excel data we will use Java and Apache POI classes.</p>
<p><strong>Below are the classes will use to read/write the data:</strong></p>
<ol>
<li>FileInputStream &#8211; A FileInputStream is an inputstream for reading data from a File.</li>
<li>XSSFWorkbook</li>
<li>XSSFSheet</li>
<li>XSSFRow</li>
<li>XSSFCell</li>
</ol>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<strong>Below are the methods will use to read/write the data which are available in the above classes:</strong></p>
<ol>
<li>getSheet(“sheetName”) &#8211; Get sheet with the given name</li>
<li>getLastCellNum() &#8211; Get the index of the last cell contained in the row Plus one as index starts from <strong>ZERO</strong></li>
<li>getLastRowNum()</li>
</ol>
<p><strong>Below is the sample Excel File:</strong></p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png"><img decoding="async" loading="lazy" class="size-full wp-image-1040 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png" alt="Sample Excel" width="706" height="374" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png 706w, https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel-300x159.png 300w" sizes="(max-width: 706px) 100vw, 706px" /></a></p>
<p>&nbsp;</p>
<p>We can download the Apache POI Jar files from this <a href="https://poi.apache.org/download.html">link</a> and can find the screenshots below for the same.</p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png"><img decoding="async" loading="lazy" class="size-full wp-image-1042 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png" alt="Apache POI Binary Distribution" width="1142" height="570" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png 1142w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-300x150.png 300w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-768x383.png 768w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-1024x511.png 1024w" sizes="(max-width: 1142px) 100vw, 1142px" /></a></p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png"><img decoding="async" loading="lazy" class="size-full wp-image-1043 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png" alt="Apache POI Download Link" width="1202" height="438" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png 1202w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-300x109.png 300w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-768x280.png 768w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-1024x373.png 1024w" sizes="(max-width: 1202px) 100vw, 1202px" /></a></p>
<p>&nbsp;</p>
<p><strong>Points to Remember:</strong></p>
<ol>
<li>Row index starts from <strong>ZERO</strong></li>
<li>Column index starts from <strong>ZERO</strong></li>
<li>Extension of excel file is ‘<strong>.xlsx</strong>’ from Excel 2007</li>
</ol>
<p>Now we will write a sample <strong>stand alone</strong> program that will give row count and column count from the excel:</p>
<pre class="brush: java; highlight: [11,12,13,14,15,16,17,18]; title: ; notranslate">
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;

public class ExcelRowAndColumnCount
{
   public static void main(String args&#x5B;]) throws Exception
   {
       FileInputStream fis = new FileInputStream(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
       XSSFWorkbook workbook = new XSSFWorkbook(fis);
       XSSFSheet sheet = workbook.getSheet(&quot;Sheet&quot;);
       XSSFRow row = sheet.getRow(0);
       int colNum = row.getLastCellNum();
       System.out.println(&quot;Total Number of Columns in the excel is : &quot;+colNum);
       int rowNum = sheet.getLastRowNum()+1;
       System.out.println(&quot;Total Number of Rows in the excel is : &quot;+rowNum);
   }
}
</pre>
<p>In the above program we have hard-coded the row number and sheet name in the program. But for the best practice we will create a util method and will call that method where ever we need. For this we will write 2 classes, one will have the actual logic to get the row count and column count from the excel and another class will call this method.<br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
Below is the excel read/write data util class. In this we will write a method called “<strong>getRowCount()</strong>” and “<strong>getColumnCount()</strong>” both will accept single parameter called <strong>sheetName</strong> from the excel sheet.</p>
<pre class="brush: java; highlight: [10,11,12,13,14,15,19,20,21,22,25,26,27,28,29,30,32,33,34,35,36,37,38]; title: ; notranslate">
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;

public class ExcelApiTest
{
   public FileInputStream fis = null;
   public XSSFWorkbook workbook = null;
   public XSSFSheet sheet = null;
   public XSSFRow row = null;
   public XSSFCell cell = null;
   String xlFilePath;

   public ExcelApiTest(String xlFilePath) throws Exception
   {
       this.xlFilePath = xlFilePath;
       fis = new FileInputStream(xlFilePath);
       workbook = new XSSFWorkbook(fis);
       fis.close();
   }

   public int getRowCount(String sheetName)
   {
       sheet = workbook.getSheet(sheetName);
       int rowCount = sheet.getLastRowNum()+1;
       return rowCount;
   }

   public int getColumnCount(String sheetName)
   {
       sheet = workbook.getSheet(sheetName);
       row = sheet.getRow(0);
       int colCount = row.getLastCellNum();
       return colCount;
   }
}
</pre>
<p>Below is a sample program which will call the methods from the above program to get the column number and row number from any excel sheet.</p>
<pre class="brush: java; highlight: [5,6,7,8,9]; title: ; notranslate">
public class RowColumnCountFromExcelUsingUtilClass
{
   public static void main(String&#x5B;] args) throws Exception
   {
       ExcelApiTest eat = new ExcelApiTest(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
       int colCount = eat.getColumnCount(&quot;Credentials&quot;);
       System.out.println(&quot;Total Columns in the Excel : &quot;+colCount);
       int rowCount = eat.getRowCount(&quot;Credentials&quot;);
       System.out.println(&quot;Total Rows in the Excel : &quot;+rowCount);
   }
}
</pre>
<p>This way we will read or get the row count and column count from the excel sheet.</p>
<p>Please watch youtube video for better and more understanding.<br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<div id="themify_builder_content-1179" data-postid="1179" class="themify_builder_content themify_builder_content-1179 themify_builder themify_builder_front">

	</div>
<!-- /themify_builder_content --><p>The post <a rel="nofollow" href="https://automationtesting.in/row-count-and-column-count-in-excel/">Row Count And Column Count in Excel</a> appeared first on <a rel="nofollow" href="https://automationtesting.in">Selenium Webdriver Appium Complete Tutorial</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://automationtesting.in/row-count-and-column-count-in-excel/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Write Data To Excel Using Column Name</title>
		<link>https://automationtesting.in/write-data-to-excel-using-column-name/</link>
					<comments>https://automationtesting.in/write-data-to-excel-using-column-name/#respond</comments>
		
		<dc:creator><![CDATA[Krishna Sakinala]]></dc:creator>
		<pubDate>Fri, 06 Jan 2017 03:27:26 +0000</pubDate>
				<category><![CDATA[Excel - Java]]></category>
		<category><![CDATA[Automation (Industry)]]></category>
		<category><![CDATA[column name]]></category>
		<category><![CDATA[Data-driven Testing]]></category>
		<category><![CDATA[how to write data to excel]]></category>
		<category><![CDATA[Microsoft Excel (Software)]]></category>
		<category><![CDATA[Selenium (Software)]]></category>
		<category><![CDATA[Software Framework (Software Genre)]]></category>
		<category><![CDATA[Webdriver]]></category>
		<category><![CDATA[write data to excel in java]]></category>
		<category><![CDATA[write data to excel in selenium]]></category>
		<category><![CDATA[write data to excel using column name]]></category>
		<guid isPermaLink="false">http://automationtesting.in/?p=1105</guid>

					<description><![CDATA[<p>Write data to excel using column name will discuss about how we can write test data/test results to the excel sheet while automating any application using selenium web driver. We might get doubt that why we need to put the test results in excel. If an application need so much of data to be feed [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://automationtesting.in/write-data-to-excel-using-column-name/">Write Data To Excel Using Column Name</a> appeared first on <a rel="nofollow" href="https://automationtesting.in">Selenium Webdriver Appium Complete Tutorial</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
Write data to excel using column name will discuss about how we can write test data/test results to the excel sheet while automating any application using selenium web driver. We might get doubt that why we need to put the test results in excel. If an application need so much of data to be feed to test; then we will manage those data using external sources like text file, xml file and excel file etc… . Among these; Excel is very user friendly that we can organize the data very easily and we can read and write the data how we want. So, we mostly use excel to maintain the test data and results. In order to create a data driven framework then it is easy with using excel.</p>
<div class="post-video"><iframe loading="lazy" width="1165" height="874" src="https://www.youtube.com/embed/tmWrTtlq7CM?wmode=transparent&#038;fs=1"  allowfullscreen></iframe></div>
<p>So, if you want to write data to excel; we need to use some third party API which are available. In those one of the famous is <strong>Apache POI</strong>. In this blog we will use Apache POI to write the data to the excel sheet. To interact with the excel, Apache POI given so many classes to interact with the excel and read and write the data. To read/write excel data we will use Java and Apache POI classes.</p>
<p><strong>Below are the classes will use to read/write the data:</strong></p>
<ol>
<li>FileInputStream &#8211; A FileInputStream is an inputstream for reading data from a File.</li>
<li>FileOutputStream &#8211; A FileOutputStream is an outputstream for writing data to a File.</li>
<li>XSSFWorkbook</li>
<li>XSSFSheet</li>
<li>XSSFRow</li>
<li>XSSFCell</li>
<li>XSSFFont</li>
<li>XSSFCellStyle</li>
</ol>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<strong>Below are the methods will use to read/write the data which are available in the above classes:</strong></p>
<ol>
<li>getSheet(“sheetName”) &#8211; Get sheet with the given name</li>
<li>getLastCellNum() &#8211; Get the index of the last cell contained in the row Plus one as index starts from <strong>ZERO</strong></li>
<li>setCellValue() &#8211; Set the value of the cell</li>
<li>getRow(int) &#8211; Returns the row.</li>
<li>getCell(int) &#8211; Get the cell representing a given <strong>column</strong></li>
</ol>
<p><strong>Below is the sample Excel File:</strong></p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png"><img decoding="async" loading="lazy" class="size-full wp-image-1040 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png" alt="Sample Excel" width="706" height="374" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png 706w, https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel-300x159.png 300w" sizes="(max-width: 706px) 100vw, 706px" /></a></p>
<p>We can download the Apache POI Jar files from this <a href="https://poi.apache.org/download.html">link</a> and can find the screenshots below for the same.</p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png"><img decoding="async" loading="lazy" class="size-full wp-image-1042 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png" alt="Apache POI Binary Distribution" width="1142" height="570" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png 1142w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-300x150.png 300w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-768x383.png 768w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-1024x511.png 1024w" sizes="(max-width: 1142px) 100vw, 1142px" /></a></p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png"><img decoding="async" loading="lazy" class="size-full wp-image-1043 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png" alt="Apache POI Download Link" width="1202" height="438" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png 1202w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-300x109.png 300w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-768x280.png 768w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-1024x373.png 1024w" sizes="(max-width: 1202px) 100vw, 1202px" /></a></p>
<p><strong>Points to Remember:</strong></p>
<ol>
<li>Row index starts from <strong>ZERO</strong></li>
<li>Column index starts from <strong>ZERO</strong></li>
<li>Extension of excel file is ‘<strong>.xlsx</strong>’ from Excel 2007</li>
</ol>
<p>Now we will write a sample stand alone program that will write the data to the excel:</p>
<pre class="brush: java; highlight: [11,12,13,14,15,16,17,18,20,21,22,23,24,25,26,27,28,30,31,32,34,35,36,38,39,40,41,43,44,45,47,48,50,51,52]; title: ; notranslate">
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class WriteDataToExcel
{
    public static void main(String args&#x5B;]) throws Exception
    {
        FileInputStream fis = new FileInputStream(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
        FileOutputStream fos = null;
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheet(&quot;Credentials&quot;);
        XSSFRow row = null;
        XSSFCell cell = null;
        XSSFFont font = workbook.createFont();
        XSSFCellStyle style = workbook.createCellStyle();

        int col_Num = -1;
        row = sheet.getRow(0);
        for(int i = 0; i &lt; row.getLastCellNum(); i++)
        {
            if(row.getCell(i).getStringCellValue().trim().equals(&quot;Result&quot;))
            {
                col_Num = i;
            }
        }

        row = sheet.getRow(1);
        if(row == null)
            row = sheet.createRow(1);

        cell = row.getCell(col_Num);
        if(cell == null)
            cell = row.createCell(col_Num);

        font.setFontName(&quot;Comic Sans MS&quot;);
        font.setFontHeight(14.0);
        font.setBold(true);
        font.setColor(HSSFColor.WHITE.index);

        style.setFont(font);
        style.setFillForegroundColor(HSSFColor.GREEN.index);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        cell.setCellStyle(style);
        cell.setCellValue(&quot;PASS&quot;);

        fos = new FileOutputStream(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
        workbook.write(fos);
        fos.close();
    }
}
</pre>
<p>In the above program we have hard-coded the row number and column names in the program. But for the best practice we will create a util method and will call that method where ever we need. For this we will write 2 classes, one will have the actual logic to read/write the excel data and another class will call this method.<br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
Below is the excel read/write data util class. In this we will write a method called “<strong>setCellData()</strong>” and it will accept 4 parameters called <strong>sheetName</strong>, <strong>columnName</strong>, <strong>rowNumber</strong> and <strong>value</strong> which is written to the excel.</p>
<pre class="brush: java; highlight: [12,13,14,15,16,17,18,22,23,24,25,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63]; title: ; notranslate">
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ExcelApiTest
{
    public FileInputStream fis = null;
    public FileOutputStream fos = null;
    public XSSFWorkbook workbook = null;
    public XSSFSheet sheet = null;
    public XSSFRow row = null;
    public XSSFCell cell = null;
    String xlFilePath;

    public ExcelApiTest(String xlFilePath) throws Exception
    {
        this.xlFilePath = xlFilePath;
        fis = new FileInputStream(xlFilePath);
        workbook = new XSSFWorkbook(fis);
        fis.close();
    }

    public boolean setCellData(String sheetName, String colName, int rowNum, String value)
    {
        try
        {
            int col_Num = -1;
            sheet = workbook.getSheet(sheetName);

            row = sheet.getRow(0);
            for (int i = 0; i &lt; row.getLastCellNum(); i++) {
                if (row.getCell(i).getStringCellValue().trim().equals(colName))
                {
                    col_Num = i;
                }
            }

            sheet.autoSizeColumn(col_Num);
            row = sheet.getRow(rowNum - 1);
            if(row==null)
                row = sheet.createRow(rowNum - 1);

            cell = row.getCell(col_Num);
            if(cell == null)
                cell = row.createCell(col_Num);

            cell.setCellValue(value);

            fos = new FileOutputStream(xlFilePath);
            workbook.write(fos);
            fos.close();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return  false;
        }
        return true;
    }
}
</pre>
<p>Below is a sample program which will call the method from the above program to write data to excel using column number:</p>
<pre class="brush: java; highlight: [5,6]; title: ; notranslate">
public class WriteDataToExcelUsingUtilClass
{
    public static void main(String args&#x5B;]) throws Exception
    {
        ExcelApiTest eat = new ExcelApiTest(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
        eat.setCellData(&quot;Credentials&quot;,&quot;Result&quot;,2,&quot;PASS&quot;);
    }
}
</pre>
<p>This way we will write data to the excel using column number.</p>
<p>Please watch youtube video for better and more understanding.<br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<div id="themify_builder_content-1105" data-postid="1105" class="themify_builder_content themify_builder_content-1105 themify_builder themify_builder_front">

	</div>
<!-- /themify_builder_content --><p>The post <a rel="nofollow" href="https://automationtesting.in/write-data-to-excel-using-column-name/">Write Data To Excel Using Column Name</a> appeared first on <a rel="nofollow" href="https://automationtesting.in">Selenium Webdriver Appium Complete Tutorial</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://automationtesting.in/write-data-to-excel-using-column-name/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Write Data To Excel Using Column Number</title>
		<link>https://automationtesting.in/write-data-to-excel-using-column-number/</link>
					<comments>https://automationtesting.in/write-data-to-excel-using-column-number/#respond</comments>
		
		<dc:creator><![CDATA[Krishna Sakinala]]></dc:creator>
		<pubDate>Thu, 05 Jan 2017 01:33:06 +0000</pubDate>
				<category><![CDATA[Excel - Java]]></category>
		<category><![CDATA[Automation (Industry)]]></category>
		<category><![CDATA[column number]]></category>
		<category><![CDATA[Data-driven Testing]]></category>
		<category><![CDATA[how to write data to excel]]></category>
		<category><![CDATA[Microsoft Excel (Software)]]></category>
		<category><![CDATA[Selenium (Software)]]></category>
		<category><![CDATA[Software Framework (Software Genre)]]></category>
		<category><![CDATA[Webdriver]]></category>
		<category><![CDATA[write data to excel in java]]></category>
		<category><![CDATA[write data to excel in selenium]]></category>
		<category><![CDATA[write data to excel using column number]]></category>
		<guid isPermaLink="false">http://automationtesting.in/?p=1102</guid>

					<description><![CDATA[<p>Write data to excel using column number will discuss about how we can write test data/test results to the excel sheet while automating any application using selenium web driver. We might get doubt that why we need to put the test results in excel. If an application need so much of data to be feed [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://automationtesting.in/write-data-to-excel-using-column-number/">Write Data To Excel Using Column Number</a> appeared first on <a rel="nofollow" href="https://automationtesting.in">Selenium Webdriver Appium Complete Tutorial</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><img src='http://automationtesting.in/wp-content/uploads/2016/12/WRITE-DATA-TO-EXCEL-USING-COLUMN-NUMBER.png'></p><p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9173866185064071"
     data-ad-slot="6638633547"
     data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
Write data to excel using column number will discuss about how we can write test data/test results to the excel sheet while automating any application using selenium web driver. We might get doubt that why we need to put the test results in excel. If an application need so much of data to be feed to test; then we will manage those data using external sources like text file, XML file and excel file etc… . Among these; Excel is very user friendly that we can organize the data very easily and we can read and write the data how we want. So, we mostly use excel to maintain the test data and results. In order to create a data driven framework then it is easy with using excel.</p>
<div class="post-video"><iframe loading="lazy" width="1165" height="874" src="https://www.youtube.com/embed/s8IXZKctlzs?wmode=transparent&#038;fs=1"  allowfullscreen></iframe></div>
<p>So, if you want to write data to excel; we need to use some third party API which are available. In those one of the famous is Apache POI. In this blog we will use Apache POI to write the data to the excel sheet. To interact with the excel, Apache POI given so many classes to interact with the excel and read and write the data. To read/write excel data we will use Java and Apache POI classes.</p>
<p><strong>Below are the classes will use to read/write the data:</strong></p>
<ol>
<li>FileInputStream &#8211; A FileInputStream is an inputstream for reading data from a File.</li>
<li>FileOutputStream &#8211; A FileOutputStream is an outputstream for writing data to a File.</li>
<li>XSSFWorkbook</li>
<li>XSSFSheet</li>
<li>XSSFRow</li>
<li>XSSFCell</li>
<li>XSSFFont</li>
<li>XSSFCellStyle</li>
</ol>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9173866185064071"
     data-ad-slot="6638633547"
     data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<strong>Below are the methods will use to read/write the data which are available in the above classes:</strong></p>
<ol>
<li>getSheet(“sheetName”) &#8211; Get sheet with the given name</li>
<li>getLastCellNum() &#8211; Get the index of the last cell contained in the row Plus one as index starts from <strong>ZERO</strong></li>
<li>setCellValue() &#8211; Set the value of the cell</li>
<li>getRow(int) &#8211; Returns the row.</li>
<li>getCell(int) &#8211; Get the cell representing a given <strong>column</strong></li>
</ol>
<p><strong>Below is the sample Excel File:</strong></p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png"><img decoding="async" loading="lazy" class="size-full wp-image-1040 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png" alt="Sample Excel" width="706" height="374" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png 706w, https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel-300x159.png 300w" sizes="(max-width: 706px) 100vw, 706px" /></a></p>
<p>&nbsp;</p>
<p>We can download the Apache POI Jar files from this <a href="https://poi.apache.org/download.html">link</a> and can find the screenshots below for the same.</p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png"><img decoding="async" loading="lazy" class="size-full wp-image-1042 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png" alt="Apache POI Binary Distribution" width="1142" height="570" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png 1142w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-300x150.png 300w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-768x383.png 768w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-1024x511.png 1024w" sizes="(max-width: 1142px) 100vw, 1142px" /></a></p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9173866185064071"
     data-ad-slot="6638633547"
     data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png"><img decoding="async" loading="lazy" class="size-full wp-image-1043 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png" alt="Apache POI Download Link" width="1202" height="438" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png 1202w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-300x109.png 300w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-768x280.png 768w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-1024x373.png 1024w" sizes="(max-width: 1202px) 100vw, 1202px" /></a></p>
<p><strong>Points to Remember:</strong></p>
<ol>
<li>Row index starts from <strong>ZERO</strong></li>
<li>Column index starts from <strong>ZERO</strong></li>
<li>Extension of excel file is ‘<strong>.xlsx</strong>’ from Excel 2007</li>
</ol>
<p>Now we will write a sample <strong>stand alone</strong> program that will write the data to the excel:</p>
<pre class="brush: java; highlight: [11,12,13,14,15,16,17,18,20,21,22,24,25,26,28,29,30,31,33,34,35,37,38,40,41,42]; title: ; notranslate">
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class WriteDataToExcel
{
    public static void main(String args&#x5B;]) throws Exception
    {
        FileInputStream fis = new FileInputStream(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
        FileOutputStream fos = null;
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheet(&quot;Credentials&quot;);
        XSSFRow row = null;
        XSSFCell cell = null;
        XSSFFont font = workbook.createFont();
        XSSFCellStyle style = workbook.createCellStyle();

        row = sheet.getRow(1);
        if(row == null)
            row = sheet.createRow(1);

        cell = row.getCell(4);
        if(cell == null)
            cell = row.createCell(4);

        font.setFontName(&quot;Comic Sans MS&quot;);
        font.setFontHeight(14.0);
        font.setBold(true);
        font.setColor(HSSFColor.WHITE.index);

        style.setFont(font);
        style.setFillForegroundColor(HSSFColor.GREEN.index);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        cell.setCellStyle(style);
        cell.setCellValue(&quot;PASS&quot;);

        fos = new FileOutputStream(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
        workbook.write(fos);
        fos.close();
    }
}
</pre>
<p>In the above program we have hard-coded the row and column numbers in the program. But for the best practice we will create a util method and will call that method where ever we need. For this we will write 2 classes, one will have the actual logic to read/write the excel data and another class will call this method.<br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9173866185064071"
     data-ad-slot="6638633547"
     data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
Below is the excel read/write data util class. In this we will write a method called “<strong>setCellData()</strong>” and it will accept 4 parameters called <strong>sheetName</strong>, <strong>columnNumber</strong>, <strong>rowNumber</strong> and <strong>value</strong> which is written to the excel.</p>
<pre class="brush: java; highlight: [12,13,14,15,16,17,18,22,23,24,25,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53]; title: ; notranslate">
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ExcelApiTest
{
    public FileInputStream fis = null;
    public FileOutputStream fos = null;
    public XSSFWorkbook workbook = null;
    public XSSFSheet sheet = null;
    public XSSFRow row = null;
    public XSSFCell cell = null;
    String xlFilePath;

    public ExcelApiTest(String xlFilePath) throws Exception
    {
        this.xlFilePath = xlFilePath;
        fis = new FileInputStream(xlFilePath);
        workbook = new XSSFWorkbook(fis);
        fis.close();
    }

    public boolean setCellData(String sheetName, int colNumber, int rowNum, String value)
    {
        try
        {
            sheet = workbook.getSheet(sheetName);
            row = sheet.getRow(rowNum);
            if(row==null)
                row = sheet.createRow(rowNum);

            cell = row.getCell(colNumber);
            if(cell == null)
                cell = row.createCell(colNumber);

            cell.setCellValue(value);

            fos = new FileOutputStream(xlFilePath);
            workbook.write(fos);
            fos.close();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return  false;
        }
        return true;
    }
}
</pre>
<p>Below is a sample program which will call the method from the above program to write data to excel using column number:</p>
<pre class="brush: java; highlight: [5,6]; title: ; notranslate">
public class WriteDataToExcelUsingUtilClass
{
    public static void main(String args&#x5B;]) throws Exception
    {
        ExcelApiTest eat = new ExcelApiTest(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
        eat.setCellData(&quot;Credentials&quot;,4,1,&quot;PASS&quot;);
    }
}
</pre>
<p>This way we will write data to the excel using column number.</p>
<p>Please watch youtube video for better and more understanding.<br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9173866185064071"
     data-ad-slot="6638633547"
     data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<div id="themify_builder_content-1102" data-postid="1102" class="themify_builder_content themify_builder_content-1102 themify_builder themify_builder_front">

	</div>
<!-- /themify_builder_content --><p>The post <a rel="nofollow" href="https://automationtesting.in/write-data-to-excel-using-column-number/">Write Data To Excel Using Column Number</a> appeared first on <a rel="nofollow" href="https://automationtesting.in">Selenium Webdriver Appium Complete Tutorial</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://automationtesting.in/write-data-to-excel-using-column-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Read Data From Excel Using Column Name</title>
		<link>https://automationtesting.in/read-data-from-excel-using-column-name/</link>
					<comments>https://automationtesting.in/read-data-from-excel-using-column-name/#comments</comments>
		
		<dc:creator><![CDATA[Krishna Sakinala]]></dc:creator>
		<pubDate>Wed, 28 Dec 2016 07:45:31 +0000</pubDate>
				<category><![CDATA[Excel - Java]]></category>
		<category><![CDATA[Automation (Industry)]]></category>
		<category><![CDATA[Data-driven Testing]]></category>
		<category><![CDATA[how to read data from excel]]></category>
		<category><![CDATA[Microsoft Excel (Software)]]></category>
		<category><![CDATA[read data from excel in java]]></category>
		<category><![CDATA[read data from excel in selenium]]></category>
		<category><![CDATA[Selenium (Software)]]></category>
		<category><![CDATA[Software Framework (Software Genre)]]></category>
		<category><![CDATA[Webdriver]]></category>
		<guid isPermaLink="false">http://automationtesting.in/?p=1094</guid>

					<description><![CDATA[<p>Read data from excel using column name will discuss about how we can read test data from the excel sheet while automating any application using selenium webdriver. We might get doubt that why we need to put the test data in excel and why to read data from that. If an application need so much [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://automationtesting.in/read-data-from-excel-using-column-name/">Read Data From Excel Using Column Name</a> appeared first on <a rel="nofollow" href="https://automationtesting.in">Selenium Webdriver Appium Complete Tutorial</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
Read data from excel using column name will discuss about how we can read test data from the excel sheet while automating any application using selenium webdriver. We might get doubt that why we need to put the test data in excel and why to read data from that. If an application need so much of data to be feeded to test; then we will manage those data using external sources like text file, xml file and excel file etc… . Among these; Excel is very user friendly that we can organize the data very easily and we can read the data how we want. So, we mostly use excel to maintain the test data. In order to create a data driven framework then it is easy with using excel.</p>
<div class="post-video"><iframe loading="lazy" width="1165" height="874" src="https://www.youtube.com/embed/RvGhtN363-U?wmode=transparent&#038;fs=1"  allowfullscreen></iframe></div>
<p>So, if you want to read data from excel; we need to use some third party API which are available. In those one of the famous is <strong>Apache POI</strong>. In this blog we will use Apache POI to read the data from the excel sheet. To interact with the excel, Apache POI given so many classes to interact with the excel and get the data. To read excel data we will use Java and Apache POI classes.</p>
<p><strong>Below are the classes will use to read the data:</strong></p>
<ol>
<li>FileInputStream &#8211; A FileInputStream is an inputstream for reading data from a File.</li>
<li>XSSFWorkbook</li>
<li>XSSFSheet</li>
<li>XSSFRow</li>
<li>XSSFCell</li>
</ol>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<strong>Below are the methods will use to read the data which are available in the above classes:</strong></p>
<ol>
<li>getSheet(“sheetName”) &#8211; Get sheet with the given name</li>
<li>getLastCellNum() &#8211; Get the index of the last cell contained in the row Plus one as index starts from ZERO</li>
<li>getStringCellValue() &#8211; Get the value of the cell as a String</li>
<li>getRow(int) &#8211; Returns the row.</li>
<li>getCell(int) &#8211; Get the cell representing a given column</li>
<li>getNumericCellValue() &#8211; Get the value of the cell as a number.</li>
<li>getDateCellValue() &#8211; Get the value of the cell as a date.</li>
<li>getBooleanCellValue() &#8211; Get the value of the cell as a boolean.</li>
</ol>
<p><strong>Below is the sample Excel File:</strong></p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png"><img decoding="async" loading="lazy" class="size-full wp-image-1040 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png" alt="Sample Excel" width="706" height="374" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png 706w, https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel-300x159.png 300w" sizes="(max-width: 706px) 100vw, 706px" /></a></p>
<p>We can download the Apache POI Jar files from this <a href="https://poi.apache.org/download.html">link</a> and can find the screenshots below for the same.</p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png"><img decoding="async" loading="lazy" class="size-full wp-image-1042 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png" alt="Apache POI Binary Distribution" width="1142" height="570" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png 1142w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-300x150.png 300w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-768x383.png 768w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-1024x511.png 1024w" sizes="(max-width: 1142px) 100vw, 1142px" /></a></p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png"><img decoding="async" loading="lazy" class="size-full wp-image-1043 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png" alt="Apache POI Download Link" width="1202" height="438" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png 1202w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-300x109.png 300w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-768x280.png 768w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-1024x373.png 1024w" sizes="(max-width: 1202px) 100vw, 1202px" /></a></p>
<p><strong>Points to Remember:</strong></p>
<ol>
<li>Row index starts from <strong>ZERO</strong></li>
<li>Column index starts from <strong>ZERO</strong></li>
<li>Extension of excel file is ‘<strong>.xlsx</strong>’ from Excel 2007</li>
<li>If you want to read integer value then you have to use “‘”(i.e. <strong>apostrophe</strong>) value in front of that value.[Example : 10 → <strong>‘</strong>10]</li>
<li>We need to use different kind of methods to read String, Integer and Date values from the excel.</li>
</ol>
<p>Now we will write a sample <strong>stand alone</strong> program that will read the data from the excel:</p>
<pre class="brush: java; highlight: [12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]; title: ; notranslate">
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;

public class ReadExcelData
{
    public static void main(String args&#x5B;]) throws Exception
    {
        FileInputStream fis = new FileInputStream(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheet(&quot;Credentials&quot;);
        XSSFRow row = sheet.getRow(0);

        int col_num = -1;

        for(int i=0; i &lt; row.getLastCellNum(); i++)
        {
            if(row.getCell(i).getStringCellValue().trim().equals(&quot;UserName&quot;))
                col_num = i;
        }

        row = sheet.getRow(1);
        XSSFCell cell = row.getCell(col_num);

        String value = cell.getStringCellValue();
        System.out.println(&quot;Value of the Excel Cell is - &quot;+ value);
    }
}
</pre>
<p>In the above program we have hard-coded the row and column numbers in the program. But for the best practice we will create a util method and will call that method where ever we need. For this we will write 2 classes, one will have the actual logic to read the excel data and another class will call this method.<br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p>Below is the excel read data util class. In this we will write a method called “<strong>getCellData()</strong>” and it will accept 3 parameters called <strong>sheetName</strong>, <strong>columnName</strong> and <strong>rowNumber</strong>.</p>
<pre class="brush: java; highlight: [15,16,17,18,19,23,24,25,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65]; title: ; notranslate">
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ExcelApiTest
{
    public FileInputStream fis = null;
    public XSSFWorkbook workbook = null;
    public XSSFSheet sheet = null;
    public XSSFRow row = null;
    public XSSFCell cell = null;

    public ExcelApiTest(String xlFilePath) throws Exception
    {
        fis = new FileInputStream(xlFilePath);
        workbook = new XSSFWorkbook(fis);
        fis.close();
    }

    public String getCellData(String sheetName, String colName, int rowNum)
    {
        try
        {
            int col_Num = -1;
            sheet = workbook.getSheet(sheetName);
            row = sheet.getRow(0);
            for(int i = 0; i &lt; row.getLastCellNum(); i++)
            {
                if(row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
                    col_Num = i;
            }

            row = sheet.getRow(rowNum - 1);
            cell = row.getCell(col_Num);

            if(cell.getCellTypeEnum() == CellType.STRING)
                return cell.getStringCellValue();
            else if(cell.getCellTypeEnum() == CellType.NUMERIC || cell.getCellTypeEnum() == CellType.FORMULA)
            {
                String cellValue = String.valueOf(cell.getNumericCellValue());
                if(HSSFDateUtil.isCellDateFormatted(cell))
                {
                    DateFormat df = new SimpleDateFormat(&quot;dd/MM/yy&quot;);
                    Date date = cell.getDateCellValue();
                    cellValue = df.format(date);
                }
                return cellValue;
            }else if(cell.getCellTypeEnum() == CellType.BLANK)
                return &quot;&quot;;
            else
                return String.valueOf(cell.getBooleanCellValue());
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return &quot;row &quot;+rowNum+&quot; or column &quot;+colNum +&quot; does not exist  in Excel&quot;;
        }
    }
}
</pre>
<p>Below is a sample which will call the method from the above program to read the excel data using column number:</p>
<pre class="brush: java; highlight: [5,6,7,8,9]; title: ; notranslate">
public class ReadExcelDataUsingUtilClass
{
    public static void main(String args&#x5B;]) throws Exception
    {
        ExcelApiTest eat = new ExcelApiTest(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);        
        System.out.println(eat.getCellData(&quot;Credentials&quot;,&quot;UserName&quot;,2));
        System.out.println(eat.getCellData(&quot;Credentials&quot;,&quot;PassWord&quot;,2));
        System.out.println(eat.getCellData(&quot;Credentials&quot;,&quot;DateCreated&quot;,2));
        System.out.println(eat.getCellData(&quot;Credentials&quot;,&quot;NoOfAttempts&quot;,2));
    }
}
</pre>
<p>This way we will read the excel data using column name.</p>
<p>Please watch youtube video for better and more understanding.<br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<div id="themify_builder_content-1094" data-postid="1094" class="themify_builder_content themify_builder_content-1094 themify_builder themify_builder_front">

	</div>
<!-- /themify_builder_content --><p>The post <a rel="nofollow" href="https://automationtesting.in/read-data-from-excel-using-column-name/">Read Data From Excel Using Column Name</a> appeared first on <a rel="nofollow" href="https://automationtesting.in">Selenium Webdriver Appium Complete Tutorial</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://automationtesting.in/read-data-from-excel-using-column-name/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Read Data From Excel Using Column Number</title>
		<link>https://automationtesting.in/read-data-from-excel-using-column-number/</link>
					<comments>https://automationtesting.in/read-data-from-excel-using-column-number/#respond</comments>
		
		<dc:creator><![CDATA[Krishna Sakinala]]></dc:creator>
		<pubDate>Sun, 18 Dec 2016 00:05:39 +0000</pubDate>
				<category><![CDATA[Excel - Java]]></category>
		<category><![CDATA[Automation (Industry)]]></category>
		<category><![CDATA[Data-driven Testing]]></category>
		<category><![CDATA[Microsoft Excel (Software)]]></category>
		<category><![CDATA[Selenium (Software)]]></category>
		<category><![CDATA[Software Framework (Software Genre)]]></category>
		<category><![CDATA[Webdriver]]></category>
		<guid isPermaLink="false">http://automationtesting.in/?p=1039</guid>

					<description><![CDATA[<p>Read data from excel using column number will discuss about how we can read test data from the excel sheet while automating any application using selenium webdriver. We might get doubt that why we need to put the test data in excel and why to read data from that. If an application need so much [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://automationtesting.in/read-data-from-excel-using-column-number/">Read Data From Excel Using Column Number</a> appeared first on <a rel="nofollow" href="https://automationtesting.in">Selenium Webdriver Appium Complete Tutorial</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><img src='http://automationtesting.in/wp-content/uploads/2016/12/READ-EXCEL-DATA-USING-COLUMN-NUMBER.png'></p><p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
Read data from excel using column number will discuss about how we can read test data from the excel sheet while automating any application using selenium webdriver. We might get doubt that why we need to put the test data in excel and why to read data from that. If an application need so much of data to be feed to test; then we will manage those data using external sources like text file, xml file and excel file etc… . Among these; Excel is very user friendly that we can organize the data very easily and we can read the data how we want. So, we mostly use excel to maintain the test data. In order to create a data driven framework then it is easy with using excel.</p>
<div class="post-video"><iframe loading="lazy" width="1165" height="874" src="https://www.youtube.com/embed/GxCS_b3rRDc?wmode=transparent&#038;fs=1"  allowfullscreen></iframe></div>
<p>So, if you want to read data from excel; we need to use some third party API which are available. In those one of the famous is Apache POI. In this blog we will use Apache POI to read the data from the excel sheet. To interact with the excel, Apache POI given so many classes to interact with the excel and get the data. To read excel data we will use Java and Apache POI classes.</p>
<p><strong>Below are the classes will use to read the data:</strong></p>
<ol>
<li>FileInputStream &#8211; A FileInputStream is an inputstream for reading data from a File.</li>
<li>XSSFWorkbook</li>
<li>XSSFSheet</li>
<li>XSSFRow</li>
<li>XSSFCell</li>
</ol>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<strong>Below are the methods will use to read the data which are available in the above classes:</strong></p>
<ol>
<li>getSheet(“sheetName”) &#8211; Get sheet with the given name</li>
<li>getLastCellNum() &#8211; Get the index of the last cell contained in the row Plus one as index starts from ZERO</li>
<li>getStringCellValue() &#8211; Get the value of the cell as a String</li>
<li>getRow(int) &#8211; Returns the row.</li>
<li>getCell(int) &#8211; Get the cell representing a given column</li>
<li>getNumericCellValue() &#8211; Get the value of the cell as a number.</li>
<li>getDateCellValue() &#8211; Get the value of the cell as a date.</li>
<li>getBooleanCellValue() &#8211; Get the value of the cell as a boolean.</li>
</ol>
<p><strong>Below is the sample Excel File:</strong></p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png"><img decoding="async" loading="lazy" class="size-full wp-image-1040 aligncenter" src="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png" alt="Sample Excel" width="706" height="374" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel.png 706w, https://automationtesting.in/wp-content/uploads/2016/12/Sample-Excel-300x159.png 300w" sizes="(max-width: 706px) 100vw, 706px" /></a></p>
<p>We can download the Apache POI Jar files from this link and can find the screenshots below for the same.</p>
<p><a href="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png"><img decoding="async" loading="lazy" class="alignnone size-full wp-image-1042" src="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png" alt="Apache POI Binary Distribution" width="1142" height="570" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution.png 1142w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-300x150.png 300w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-768x383.png 768w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Binary-Distribution-1024x511.png 1024w" sizes="(max-width: 1142px) 100vw, 1142px" /></a><br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
<a href="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png"><img decoding="async" loading="lazy" class="alignnone size-full wp-image-1043" src="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png" alt="Apache POI Download Link" width="1202" height="438" srcset="https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link.png 1202w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-300x109.png 300w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-768x280.png 768w, https://automationtesting.in/wp-content/uploads/2016/12/Apache-POI-Download-Link-1024x373.png 1024w" sizes="(max-width: 1202px) 100vw, 1202px" /></a></p>
<p><strong>Points to Remember:</strong></p>
<ol>
<li>Row index starts from <strong>ZERO</strong></li>
<li>Column index starts from <strong>ZERO</strong></li>
<li>Extension of excel file is ‘<strong>.xlsx</strong>’ from Excel 2007</li>
<li>If you want to read integer value then you have to use “<strong>‘</strong>”(i.e. <strong>apostrophe</strong>) value in front of that value.[Example : 10 → <strong>‘</strong>10]</li>
<li>We need to use different kind of methods to read String, Integer and Date values from the excel.</li>
</ol>
<p>Now we will write a sample <strong>stand alone</strong> program that will read the data from the excel:</p>
<pre class="brush: java; highlight: [11,12,13,14,15,17,18]; title: ; notranslate">
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;

public class ReadExcelData
{
    public static void main(String args&#x5B;]) throws Exception
    {
        FileInputStream fis = new FileInputStream(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheet(&quot;Credentials&quot;);
        XSSFRow row = sheet.getRow(1);
        XSSFCell cell = row.getCell(0);

        String userName = String.valueOf(cell.getNumericCellValue());
        System.out.println(&quot;Value from the Excel sheet :&quot;+ userName);
    }
}
</pre>
<p>In the above program we have hard-coded the row and column numbers in the program. But for the best practice we will create a util method and will call that method where ever we need. For this we will write 2 classes, one will have the actual logic to read the excel data and another class will call this method.<br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script><br />
Below is the excel read data util class. In this we will write a method called “<strong>getCellData()</strong>” and it will accept 3 parameters called <strong>sheetName</strong>, <strong>columnNumber</strong> and <strong>rowNumber</strong>.</p>
<pre class="brush: java; highlight: [15,16,17,18,19,23,24,25,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56]; title: ; notranslate">
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ExcelApiTest
{
    public FileInputStream fis = null;
    public XSSFWorkbook workbook = null;
    public XSSFSheet sheet = null;
    public XSSFRow row = null;
    public XSSFCell cell = null;

    public ExcelApiTest(String xlFilePath) throws Exception
    {
        fis = new FileInputStream(xlFilePath);
        workbook = new XSSFWorkbook(fis);
        fis.close();
    }

    public String getCellData(String sheetName,int colNum,int rowNum)
    {
        try
        {
            sheet = workbook.getSheet(sheetName);
            row = sheet.getRow(rowNum);
            cell = row.getCell(colNum);
            if(cell.getCellTypeEnum() == CellType.STRING)
                return cell.getStringCellValue();
            else if(cell.getCellTypeEnum() == CellType.NUMERIC || cell.getCellTypeEnum() == CellType.FORMULA)
            {
                String cellValue  = String.valueOf(cell.getNumericCellValue());
                if (HSSFDateUtil.isCellDateFormatted(cell))
                {
                    DateFormat df = new SimpleDateFormat(&quot;dd/MM/yy&quot;);
                    Date date = cell.getDateCellValue();
                    cellValue = df.format(date);
                }
                return cellValue;
            }else if(cell.getCellTypeEnum() == CellType.BLANK)
                return &quot;&quot;;
            else
                return String.valueOf(cell.getBooleanCellValue());
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return &quot;row &quot;+rowNum+&quot; or column &quot;+colNum +&quot; does not exist  in Excel&quot;;
        }
    }
}
</pre>
<p>Below is a sample which will call the method from the above program to read the excel data using column number:</p>
<pre class="brush: java; highlight: [5,6,7,8,9]; title: ; notranslate">
public class ReadExcelDataUsingUtilClass
{
    public static void main(String args&#x5B;]) throws Exception
    {
        ExcelApiTest eat = new ExcelApiTest(&quot;/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx&quot;);
        System.out.println(eat.getCellData(&quot;Credentials&quot;,0,2));
        System.out.println(eat.getCellData(&quot;Credentials&quot;,1,2));
        System.out.println(eat.getCellData(&quot;Credentials&quot;,2,2));
        System.out.println(eat.getCellData(&quot;Credentials&quot;,3,2));
    }
}
</pre>
<p>This way we will read the excel data using column number.</p>
<p>Please watch youtube video for better and more understanding.<br />
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Responsive --><br />
<ins class="adsbygoogle" style="display: block;" data-ad-client="ca-pub-9173866185064071" data-ad-slot="6638633547" data-ad-format="auto"></ins><br />
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<div id="themify_builder_content-1039" data-postid="1039" class="themify_builder_content themify_builder_content-1039 themify_builder themify_builder_front">

	</div>
<!-- /themify_builder_content --><p>The post <a rel="nofollow" href="https://automationtesting.in/read-data-from-excel-using-column-number/">Read Data From Excel Using Column Number</a> appeared first on <a rel="nofollow" href="https://automationtesting.in">Selenium Webdriver Appium Complete Tutorial</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://automationtesting.in/read-data-from-excel-using-column-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
