0

I am trying to convert excel file to .csv file using ColdFusion code. I have written the following code. While I am trying to create excel COM object in server i am getting a java runtime exception like

"java.lang.RuntimeException: Can not use native code: Initialisation failed. Cannot create Excel Object"

I am using ColdFusion 8.0. Please help me to solve this exception.

Code:

<!---Converting Excel file to CSV file--->
<cfsetting enablecfoutputonly="Yes">

<!--------------------------------------------------------- Constants ---->

<!------------------------------------------------ Initial Parameters ---->
<cfparam name="sheetName" type="string" default="sheet1">
<cfparam name="showConfirmation" type="boolean" default="0">

<!--- Try to connect to the Word application object --->
<cftry>
  <!--- If it exists, connect to it --->
  <cfobject 
           action="CONNECT" 
           class="Excel.Application" 
           name="objExcel" 
           type="COM">
  <cfcatch type="ANY">
    <!--- The object doesn't exist, so create it --->
    <cftry>
      <cfobject 
               action="CREATE" 
               class="Excel.Application" 
               name="objExcel" 
               type="COM"> 
      <!--- Word isn't installed, or ColdFusion doesn't have access to it --->
      <cfcatch type="ANY">
        <cfoutput>
          <cfdump var="#cfcatch.Detail#">
          <font size="-1"><b>Cannot create Excel Object</b><br>
            <ol>
              <li>Make sure Excel is installed on the ColdFusion Server.</li>
              <li>Make sure that ColdFusion has permissions to 
                                   use the Excel COM objects.</li>
            </ol>
          </font>
        </cfoutput>
        <cfabort>
      </cfcatch>
    </cftry>
  </cfcatch>
</cftry>
<cftry>
  <cfscript>
    // this code will open up the excel spread sheet and then save it as a CSV file.

    // Excel file path
    ExcelFilePath = "#path#csar_reports\Budgeting_Requests_as of_#date#.xls";

    // Destination path for new Excel spreadsheet
    // We leave off the extension since we'll declare the file type later on
    CSVFilePath = "#path#csar_reports\Budgeting_Requests_as of_#date#.csv";

    // Open Excel in the background
    objExcel.Visible = false;  

    // Disable alerts such as: 'Save this document?'
    objExcel.DisplayAlerts = false;               

    // Get the 'Documents' collection
    objBooks = objExcel.workbooks;  

    // Open the Excel spreadsheet
    objBook = objBooks.Open(ExcelFilePath);  

    // Get the sheets collection 
    objSheets = objBook.WorkSheets;

    // Get the first sheet 
    objSheet = objSheets.Item(Val(1));   

    // Get a named sheet 
    //objSheet = objSheets.Item("#sheetName#");       


    /* Save it as a new document -- the extension will automatically
       be appended based on the file type we choose.
       Some of the file types to convert to:
       1 = .xls
       3 = .txt (tab delimetered)
       6 = .csv
    */
    objSheet.SaveAs(CSVFilePath, Val(6));  

    // Close the spreadsheet
    objBook.Close();     

    // Quit Excel
    objExcel.Quit();

    if (showConfirmation EQ 1) {
      writeOutput("<font size=""-1"" face=""Verdana,Geneva,Arial,Helvetica,sans-serif""><b>File Created:</b> ""#attributes.CSVFileOut#"" <b>from file:</b> ""#attributes.excelFileIn#""<p></font>");
    }
  </cfscript>

  <cfcatch>
    <!--- error creating file --->
    <cfoutput>
      <font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="-1">there was an error creating your 
                     new CSV filesfrom your Excel Spreadsheet
      <P>#cfcatch.message#</P> <P>Caught an exception, type = #cfcatch.type# </P> <P>The contents of the tag stack are:</P> <cfloop index = i from = 1 to = #ArrayLen(cfcatch.tagContext)#> <cfset sCurrent = #cfcatch.tagContext[i]#> <BR>#i# #sCurrent["ID"]# (#sCurrent["LINE"]#,#sCurrent["COLUMN"]#) #sCurrent["TEMPLATE"]# </cfloop> 
    </cfoutput>
  </cfcatch>
</cftry>
<cfsetting enablecfoutputonly="no">
4
  • Sandeep - what have you done to troubleshoot? Why do you believe the error you are getting is incorrect. Is the proper Excel COM dll installed on the server? Dec 10, 2014 at 14:53
  • @Mark- I have no idea why the exception is occurring. I have checked in server, MS excel is installed. Dec 10, 2014 at 14:57
  • Where did you get this code? What version of Excel is installed? Old versions use COM, newer versions .NET. There are lots of dependencies here Sandeep. It will take a lot of trial and error to make this happen. Dec 10, 2014 at 15:05
  • @SandeepReddyK. What is your server configuration?
    – Pankaj
    Dec 11, 2014 at 8:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.