Nothing too complex here. The form has a grand total of one field - the file field named xlsfile. Note above the form is a simple set of ColdFusion logic to notice an errors variable and output it. In case you're curious, this value will be created a bit later in our example. So - let's process the upload. Here's the code that handles that.
This code block begins with the field check used for our upload. If it exists, and has a value, we have to do some processing. We need a place to store the upload, and as we all know, you never upload files to a directory under web root.
Therefore I used the temp directory as a quick storage place. If the file was successfully uploaded, I use isSpreadsheetFile to determine if the file was a valid spreadsheet.
If it is a valid spreadsheet, I read it in using the cfspreadsheet tag. Notice the last two arguments. The query argument tells ColdFusion to parse the spreadsheet data into a query. This assumes we only want the first sheet.
If you want to work with other sheets, that's definitely possible. The last argument, headerrow, tells ColdFusion to consider the first row to be column headers. It may not always be advisable to assume this. But for now, we will. The rest of that block simply handles errors and specifying if we should show the form again.
If the user uploaded a valid spreadsheet we don't want to show the form. Instead, we want to display the contents. Let's look at how I did this. So skipping over the CSS, the real meat of the work begins when we get the metadata. Why do we do this? ColdFusion's query object does not maintain the same order of columns that our spreadsheet had.
I can use the getMetadata function on the query to get the proper column order. That's the array list you see there. Next - we do a quick check of the size of the query. We are assuming our spreadsheet has a first row being used as headers. So if we assume that, and there is only one row, then we really don't have any data. After that it's a simple matter of outputting the query dynamically. For an example of working with dynamic ColdFusion queries, see this blog entry.
Your homework, if you chose to accept it, is to simply take the template and add a checkbox to toggle if the code should assume the first row is the header. It's not as simple as you think.
The tag syntax depends on the action attribute value:. See also. Sreadsheet functions. ColdFusion Added the attribute autosize. ColdFusion 9. You cannot use the uppdate action to change an existing sheet in a file. For more information, see Usage. Note: Auto sizing can be relatively slow on large sheets. Specify name or query. Specify the name or query. Specify format, name, or query.
I have just begun to research, but I can already see the possibilities. It is part of the Apache Jakarta project. You can read and write binary excel files, including formulas, merging cells, formating, multiple worksheets etc. That just seems funny. The JExcel stuff is mad simple like "jxl. Why is Jakarta all about using crazy long complicated names?? Anyway, I will give it a looking into. WOW, thanks I was looking into purchasing some code that did the exact same thing.
I will check this out first. I've been using it at work for about 6 months to read and generate native Excel documents. It doesn't seem to have the second lag that you mentioned, and it is mostly logical. I will take a look. Right now, all I can really see myself doing is reading and writing simple files.
It looks like the POI stuff has some great formatting issues. Let me get a feel for both and then I can make an educated choice. Is there nothing they could have done to make that easier to type?
Granted I have very little Java experience, so I have no idea what the language forces you to do, but this just seems silly. I'd be curious to see if you still get a lag if you put the jar file in your CF classpath, and invoke it directly, without the classloader.
As for Java class naming - keep in mind that Java has an import statement - which prevents you from having to repeatedly type full package names. We don't have that luxury from CF. That might be the issue regarding the initial delay.
However, once the Java object is loaded, there should be no difference when it comes to reading the actual Excel file itself right? The file is read into a dynamic, local variable that you can use in the page.
For example:. Note: This action reads the file into a variable in the local Variables scope. It is not intended for use with large files, such as logs, because this can bring down the server. See the History section of the main cffile tag page.
The MIME content type of the page, optionally followed by a semicolon and the character encoding. However, if the file attribute is specified, ColdFusion attempts to get the content type from the file. The content type determines how the browser or client interprets the page contents. The following are some of the content type values that you can use:.
To set the character encoding character set of generated output, including the page HTML, use code such as the following:. You can use the cfcontent tag to override the default character encoding of the response. For example, to tell ColdFusion to return the page using Japanese EUC character encoding, use the type attribute, as follows:. If a file delete operation is unsuccessful, ColdFusion throws an error.
Do not use this tag after the cfflush tag on a page, it has no effect or ColdFusion throws an error.
0コメント