Question

How do I upload a excel spreadsheet into existing table using ColdFusion10? I have an an excel spreadsheet that has been saved and I have CFQuery ("myQuery") that outputs the data I need. How do I loop through the query and import into an existing table?

Database: MS SQL Server

Thus far, I understand that I need to loop through the query I have that has all the data.

    <cffunction name="uploadDogSheet" access="public" output="yes" returnType="void" 
    hint="upload the spreadSheet">

<cfset currentRowChecked = "1"> 
<cfset lastRow = numberOfRows> <!-- sets the number of rows that it will validate-->


    <cfspreadsheet
    action="read"
    src="#SESSION.theExcelFile#"
    headerrow= "1"
    excludeHeaderRow = "true"
    query = "allDoggyData"
    rows = "1-#lastRow#" />     


    <cfscript>    

        allDataQuery = new Query(
        sql ="SELECT * FROM allDoggyData",
        dbtype = "query",
        allData = allData);
        allDataQueryResult = allDataQuery.execute().getResult();
    </cfscript> 




    <cfloop query="allDoggyData">

<CFQUERY DATASOURCE="#mydatabase#" name="input_req">
insert into temp_dog_upload (dogNameColumn, dogBreedColumn, dogColor)
          values
(
<cfqueryparam value="#allDoggyData.dogNameExcelColumn#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#allDoggyData.dogBreedExcelColumn#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#allDoggyData.dogColorExcelColumn#" cfsqltype="cf_sql_varchar">
)               
</CFQUERY>

</cfloop>

<p>The sheet has been uploaded!<p></span>

Was it helpful?

Solution

What you are asking is pretty simple. Upload the spreadsheet to your sever then use cfspreadsheet to read it.

<cfspreadsheet  
action="read" 
src = "filepath" 
columns = "range" 
columnnames = "comma-delimited list" 
excludeHeaderRow = "true | false" 
format = "CSV|HTML" 
headerrow = "row number" 
name = "text" 
query = "query name" 
rows = "range" 
sheet = "number" 
sheetname = "text"> 

Then simply loop through the query that you defined in the cfspreadsheet

<cfloop query="queryname">

    <cfquery name="" datasource="">
       INSERT INTO ....
    </cfquery

</cfloop>

P.S. This was my answer BEFORE you deleted the post and I was no longer able to submit my answer.

OTHER TIPS

There are a couple of approaches to take. They both have one very important detail in common. Put the spreadsheet data into a staging table first. Process and validate as required first, and then write to your main tables from your staging table.

Method 1 is to have your web page accept files from your users and put them somewhere. Then, write an SSIS package that looks for these files, loads them into your staging table, and carrys on until the job is done. Then write an agent to schedule this job to run at the appropriate interval.

Method 2 is to use continue with ColdFusion. You have already read the spreadsheet into a query. Loop through that query to populate your staging table and carry on processing.

There are a couple of things to look out for with each method. With Method 1, there might be more than one file to be processed. Your package will have to handle that. With Method 2, there might be two users looking to process files at the same time. You'll have to make sure the two requests don't interfere with each other.

As far as your specific question of "How do I loop through the query and import into an existing table? ", like this:

<cfoutput query="yourquery">
<cfquery datasource="something">
insert into atable
(fields go here)
values
(values, using cfqueryparam go here)
</cfquery>
</cfoutput>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top