Home > io > loadWorkbook.m

loadWorkbook

PURPOSE ^

loadWorkbook

SYNOPSIS ^

function workbook=loadWorkbook(fileName,createEmpty)

DESCRIPTION ^

 loadWorkbook
   Loads an Excel file into a Workbook object using the Java library Apache POI

   fileName    name of the Excel file. If it doesn't exist it will be
               created
   createEmpty true if an empty workbook should be created if the file
               didn't exist (optional, default false)

   workbook    Workbook object representing the Excel file

 Usage: workbook=loadWorkbook(fileName,createEmpty)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function workbook=loadWorkbook(fileName,createEmpty)
0002 % loadWorkbook
0003 %   Loads an Excel file into a Workbook object using the Java library Apache POI
0004 %
0005 %   fileName    name of the Excel file. If it doesn't exist it will be
0006 %               created
0007 %   createEmpty true if an empty workbook should be created if the file
0008 %               didn't exist (optional, default false)
0009 %
0010 %   workbook    Workbook object representing the Excel file
0011 %
0012 % Usage: workbook=loadWorkbook(fileName,createEmpty)
0013 
0014 if nargin<2
0015     createEmpty=false;
0016 end
0017 
0018 %Check if the user has MATLAB Text Analytics Toolbox installed, as it comes
0019 %with its own conflicting version of the required Apache POI files
0020 if exist('vaderSentimentScores.m')== 2
0021     error(['MATLAB Text Analytics Toolbox found. This should be uninstalled ' ...
0022            'if you want to read/write Excel files. See RAVEN GitHub Issues '...
0023            'page for instructions.'])    
0024 end
0025     
0026 %Adds the required classes to the static Java path if not already added
0027 addJavaPaths();
0028 
0029 %Import required classes from Apache POI
0030 import org.apache.poi.ss.usermodel.*;
0031 import org.apache.poi.ss.util.*;
0032 import java.io.FileInputStream;
0033 import org.apache.poi.hssf.usermodel.*;
0034 import org.apache.poi.xssf.usermodel.*;
0035 
0036 %Check if the file exists
0037 if ~isfile(fileName)
0038     if createEmpty==false
0039         EM='The Excel file could not be found';
0040         dispEM(EM);
0041     else
0042         %Create an empty workbook
0043         [~,~,I]=fileparts(fileName);
0044         if strcmpi(I,'.xls')
0045             workbook=HSSFWorkbook();
0046         else
0047             if strcmpi(I,'.xlsx')
0048                 workbook=XSSFWorkbook();
0049             else
0050                 EM='The file name must end in .xls or .xlsx';
0051                 dispEM(EM);
0052             end
0053         end
0054     end
0055 else
0056     %Opens the workbook. The input stream is needed since it will otherwise
0057     %keep the file open
0058     is=FileInputStream(getFullPath(fileName));
0059     workbook=WorkbookFactory.create(is);
0060     is.close();
0061 end
0062 end

Generated by m2html © 2005