title: Fastest way to read a large excel file into databricks
body: <p>So I have been having some issues reading large excel files into databricks using pyspark and pandas. Spark seems to be really fast at csv and txt but not excel</p>
  
  <p>i.e</p>
  
  <p><code>df2=pd.read_excel(excel_file, sheetname=sheets,skiprows = skip_rows).astype(str)</code></p>
  
  <p><code>df = spark.read.format("com.crealytics.spark.excel").option("dataAddress", "\'" + sheet + "\'" + "!A1").option("useHeader","false").option("maxRowsInMemory",1000).option("inferSchema","false").load(filePath)</code></p>
  
  <p>We have found the fastest way to read in an excel file to be one which was written by a contractor:</p>
  
  <pre><code>from openpyxl import load_workbook
  import csv
  from os import sys
  excel_file = "/dbfs/{}".format(path)
  
  sheets = []
  workbook = load_workbook(excel_file,read_only=True,data_only=True)
  all_worksheets = workbook.get_sheet_names()
  
  for worksheet_name in workbook.get_sheet_names():
      print("Export " + worksheet_name + " ...")
      try:
          worksheet = workbook.get_sheet_by_name(worksheet_name)
      except KeyError:
          print("Could not find " + worksheet_name)
          sys.exit(1)
  
      with open("/dbfs/{}/{}.csv".format(tempDir, worksheet_name), 'w') as your_csv_file:
        wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
        headerDone = False
        for row in worksheet.iter_rows():
            lrow = []
  
            if headerDone == True:
              lrow.append(worksheet_name)
            else:
              lrow.append("worksheet_name")
              headerDone = True
  
            for cell in row:
                lrow.append(cell.value)
            wr.writerow(lrow)
  
  #Sometimes python gets a bit ahead of itself and 
  #tries to do this before it's finished writing the csv
  #and fails
  
  retryCount = 0
  retryMax = 20
  
  while retryCount &lt; retryMax:
      try:
        df2 = spark.read.format("csv").option("header", "true").load(tempDir)
        if df2.count() == 0:
          print("Retrying load from CSV")
          retryCount = retryCount + 1
          time.sleep(10)
        else:
          retryCount = retryMax
      except:
        print("Thew an error trying to read the file")
  </code></pre>
  
  <p>The reason it is fast is that it is only storing one line of excel sheet in memory when it loops round. I tried appending the list of rows together but this made it very slow.</p>
  
  <p>The issue with the above method is that it writing to csv and re-reading it doesn't seem the most robust method. Its possible that the csv could be read part way while its written and it could still be read in and data could be lost.</p>
  
  <p>Is there any other way of making this fast such as using cython so you can just put the append the list of rows without incurring a penalty for the memory and put them directly into spark directly via createDataFrame?</p>