<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Excel on Andrew Odendaal</title>
    <link>https://andrewodendaal.com/tags/excel/</link>
    <description>Recent content in Excel on Andrew Odendaal</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 03 Jun 2021 00:05:51 +0000</lastBuildDate>
    <atom:link href="https://andrewodendaal.com/tags/excel/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Using Apache POI to Get or Create a Sheet in Excel using Java</title>
      <link>https://andrewodendaal.com/using-apache-poi-to-get-or-create-a-sheet-in-excel-using-java/</link>
      <pubDate>Thu, 03 Jun 2021 00:05:51 +0000</pubDate>
      <guid>https://andrewodendaal.com/using-apache-poi-to-get-or-create-a-sheet-in-excel-using-java/</guid>
      <description>&lt;p&gt;&lt;!-- raw HTML omitted --&gt;Apache POI&lt;!-- raw HTML omitted --&gt; provides a mechanism to work with Excel documents.&lt;/p&gt;&#xA;&lt;p&gt;However, it has a missing feature when you want to only create a new sheet if one with that name doesn&amp;rsquo;t already exist.&lt;/p&gt;&#xA;&lt;p&gt;Luckily, it&amp;rsquo;s quite simple to get around this using the following code.&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;List&amp;lt;String&amp;gt; sheets = new ArrayList&amp;lt;&amp;gt;();&#xA;for (int i = 0; i &amp;lt; workbook.getNumberOfSheets(); i++) {&#xA;    Sheet sheet = workbook.getSheetAt(i);&#xA;    sheets.add(sheet.getSheetName());&#xA;}&#xA;Sheet sheet = sheets.contains(name) ?&#xA;                workbook.getSheet(name) :&#xA;                workbook.createSheet(name);&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The code uses the &lt;code&gt;workbook&lt;/code&gt; object you pass it, checks to see if a sheet with the specified name exists and then only creates one if needed. Otherwise it returns a reference to the existing one.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
