473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Exporting to Excel (xlsx files)

Can anoyne tell me why this VBScript will create the file to Excel
just fine, but the Excel file will not open up? I am saving it as a
xlsx file instead of an xls one and I have the new version of Excel
on
my machine and have opened other Excel files with that extension but
this one I get the following error:

"Excel cannot open the file 'Test.xlsx' because the file format or
file extension is not valid. Verify that the file has not been
corrupted and the file extension matches the format of the file."
If I switch the file type to be .xls instead of .xlsx, it will save
the file and open with no problems. Below is an example of the VB
script I'm using.
dim Cn,Rs
set Cn=server.createobject("ADODB.connection")
set Rs=server.createobject("ADODB.recordset")
Cn.open "MyConnectionString"
Rs.open "mysqlquery",Cn,1,3
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;
filename=Test.xlsx"
if Rs.eof <true then
response.write "<table border=1>"
while not Rs.eof
response.write "<tr><td>" & Rs.fields("mydatafield") & "</
td></tr>"
Rs.movenext
wend
response.write "</table>"
end if
set rs=nothing
Cn.close
Nov 19 '08 #1
5 48892
"Doogie" <dn******@dtgnet.comwrote in message
news:e5**********************************@g38g2000 yqn.googlegroups.com...
Can anoyne tell me why this VBScript will create the file to Excel
just fine, but the Excel file will not open up? I am saving it as a
xlsx file instead of an xls one and I have the new version of Excel
on
my machine and have opened other Excel files with that extension but
this one I get the following error:

"Excel cannot open the file 'Test.xlsx' because the file format or
file extension is not valid. Verify that the file has not been
corrupted and the file extension matches the format of the file."
If I switch the file type to be .xls instead of .xlsx, it will save
the file and open with no problems. Below is an example of the VB
script I'm using.
dim Cn,Rs
set Cn=server.createobject("ADODB.connection")
set Rs=server.createobject("ADODB.recordset")
Cn.open "MyConnectionString"
Rs.open "mysqlquery",Cn,1,3
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;
filename=Test.xlsx"
if Rs.eof <true then
response.write "<table border=1>"
while not Rs.eof
response.write "<tr><td>" & Rs.fields("mydatafield") & "</
td></tr>"
Rs.movenext
wend
response.write "</table>"
end if
set rs=nothing
Cn.close

Because an .xlsx file is opened strictly as a Zip package used by 2007
office applications. Whereas as an .xls file is opened by 2007 using the
same content sniffing tricks the previous versions did, hence it detects
whether the content is a proper ole storage based xls binary, a csv text
stream or a html text stream.

--
Anthony Jones - MVP ASP/ASP.NET

Nov 19 '08 #2
On Nov 19, 3:55*pm, "Anthony Jones" <AnthonyWJo...@yadayadayada.com>
wrote:
"Doogie" <dnlwh...@dtgnet.comwrote in message

news:e5**********************************@g38g2000 yqn.googlegroups.com...


Can anoyne tell me why this VBScript will create the file to Excel
just fine, but the Excel file will not open up? *I am saving it as a
xlsx file instead of an xls one and I have the new version of Excel
on
my machine and have opened other Excel files with that extension but
this one I get the following error:
"Excel cannot open the file 'Test.xlsx' because the file format or
file extension is not valid. *Verify that the file has not been
corrupted and the file extension matches the format of the file."
If I switch the file type to be .xls instead of .xlsx, it will save
the file and open with no problems. *Below is an example of the VB
script I'm using.
dim Cn,Rs
set Cn=server.createobject("ADODB.connection")
set Rs=server.createobject("ADODB.recordset")
Cn.open "MyConnectionString"
Rs.open "mysqlquery",Cn,1,3
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;
filename=Test.xlsx"
if Rs.eof <true then
* * response.write "<table border=1>"
* * while not Rs.eof
* * * * *response.write "<tr><td>" & Rs.fields("mydatafield")& "</
td></tr>"
* * * * *Rs.movenext
* * wend
* * response.write "</table>"
end if
set rs=nothing
Cn.close

Because an .xlsx file is opened strictly as a Zip package used by 2007
office applications. *Whereas as an .xls file is opened by 2007 using the
same content sniffing tricks the previous versions did, hence it detects
whether the content is a proper ole storage based xls binary, a csv text
stream or a html text stream.

--
Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -

- Show quoted text -
So is there anyway to get it to work in VB script or am I stuck using
xls? The reason I ask, is when opening up the xls file I always get a
warning message too. It still opens, but it's annoying to get the
message. The message I get is:

"The file you are trying to open, 'Test.xls' is in a different format
than specified by the file extension. Verify that the file is not
corrupted and is from a trusted source before opening the file. Do
you want to open the file now?"
Nov 19 '08 #3

"Doogie" <dn******@dtgnet.comwrote in message
news:8b**********************************@t2g2000y qm.googlegroups.com...
On Nov 19, 3:55 pm, "Anthony Jones" <AnthonyWJo...@yadayadayada.com>
wrote:
"Doogie" <dnlwh...@dtgnet.comwrote in message

news:e5**********************************@g38g2000 yqn.googlegroups.com...


Can anoyne tell me why this VBScript will create the file to Excel
just fine, but the Excel file will not open up? I am saving it as a
xlsx file instead of an xls one and I have the new version of Excel
on
my machine and have opened other Excel files with that extension but
this one I get the following error:
"Excel cannot open the file 'Test.xlsx' because the file format or
file extension is not valid. Verify that the file has not been
corrupted and the file extension matches the format of the file."
If I switch the file type to be .xls instead of .xlsx, it will save
the file and open with no problems. Below is an example of the VB
script I'm using.
dim Cn,Rs
set Cn=server.createobject("ADODB.connection")
set Rs=server.createobject("ADODB.recordset")
Cn.open "MyConnectionString"
Rs.open "mysqlquery",Cn,1,3
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;
filename=Test.xlsx"
if Rs.eof <true then
response.write "<table border=1>"
while not Rs.eof
response.write "<tr><td>" & Rs.fields("mydatafield") & "</
td></tr>"
Rs.movenext
wend
response.write "</table>"
end if
set rs=nothing
Cn.close

Because an .xlsx file is opened strictly as a Zip package used by 2007
office applications. Whereas as an .xls file is opened by 2007 using the
same content sniffing tricks the previous versions did, hence it detects
whether the content is a proper ole storage based xls binary, a csv text
stream or a html text stream.

--
Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -

- Show quoted text -
So is there anyway to get it to work in VB script or am I stuck using
xls? The reason I ask, is when opening up the xls file I always get a
warning message too. It still opens, but it's annoying to get the
message. The message I get is:
"The file you are trying to open, 'Test.xls' is in a different format
than specified by the file extension. Verify that the file is not
corrupted and is from a trusted source before opening the file. Do
you want to open the file now?"
I think that what Anthony was saying is that you are not actually creating
an xslx file. You are creating an html file, and giving it a different
extension. Previous versions of Excel were happy with this approach (to a
point), but the latest version appears not to like it at all. If you tried
to read from these "faux" Excel files using OleDb, they would also complain
that the file format was not as expected.

One way to get round this is to create a real .xslx file in Excel and use
that as a blank template, then use the ACE OleDb provider to write your data
to it in much the same way as if your were writing data to a database. The
ACE OleDb provider will need to be installed on the machine on which your
app is running for this to work.

Or you can ask yourself if you really need to try to generate the file with
an xlsx extension, since 2007 will happily open xls files and even csv
files.

--
Mike Brind
MVP - ASP/ASP.NET
Nov 20 '08 #4
On Nov 20, 1:51*am, "Mike Brind" <paxton...@hotmail.comwrote:
"Doogie" <dnlwh...@dtgnet.comwrote in message

news:8b**********************************@t2g2000y qm.googlegroups.com...
On Nov 19, 3:55 pm, "Anthony Jones" <AnthonyWJo...@yadayadayada.com>
wrote:


"Doogie" <dnlwh...@dtgnet.comwrote in message
news:e5**********************************@g38g2000 yqn.googlegroups.com....
Can anoyne tell me why this VBScript will create the file to Excel
just fine, but the Excel file will not open up? I am saving it as a
xlsx file instead of an xls one and I have the new version of Excel
on
my machine and have opened other Excel files with that extension but
this one I get the following error:
"Excel cannot open the file 'Test.xlsx' because the file format or
file extension is not valid. Verify that the file has not been
corrupted and the file extension matches the format of the file."
If I switch the file type to be .xls instead of .xlsx, it will save
the file and open with no problems. Below is an example of the VB
script I'm using.
dim Cn,Rs
set Cn=server.createobject("ADODB.connection")
set Rs=server.createobject("ADODB.recordset")
Cn.open "MyConnectionString"
Rs.open "mysqlquery",Cn,1,3
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;
filename=Test.xlsx"
if Rs.eof <true then
response.write "<table border=1>"
while not Rs.eof
response.write "<tr><td>" & Rs.fields("mydatafield") & "</
td></tr>"
Rs.movenext
wend
response.write "</table>"
end if
set rs=nothing
Cn.close
Because an .xlsx file is opened strictly as a Zip package used by 2007
office applications. Whereas as an .xls file is opened by 2007 using the
same content sniffing tricks the previous versions did, hence it detects
whether the content is a proper ole storage based xls binary, a csv text
stream or a html text stream.
--
Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -
- Show quoted text -
So is there anyway to get it to work in VB script or am I stuck using
xls? *The reason I ask, is when opening up the xls file I always get a
warning message too. *It still opens, but it's annoying to get the
message. *The message I get is:
"The file you are trying to open, 'Test.xls' is in a different format
than specified by the file extension. *Verify that the file is not
corrupted and is from a trusted source before opening the file. *Do
you want to open the file now?"

I think that what Anthony was saying is that you are not actually creating
an xslx file. *You are creating an html file, and giving it a different
extension. *Previous versions of Excel were happy with this approach (to a
point), but the latest version appears not to like it at all. *If you tried
to read from these "faux" Excel files using OleDb, they would also complain
that the file format was not as expected.

One way to get round this is to create a real .xslx file in Excel and use
that as a blank template, then use the ACE OleDb provider to write your data
to it in much the same way as if your were writing data to a database. *The
ACE OleDb provider will need to be installed on the machine on which your
app is running for this to work.

Or you can ask yourself if you really need to try to generate the file with
an xlsx extension, since 2007 will happily open xls files and even csv
files.

--
Mike Brind
MVP - ASP/ASP.NET- Hide quoted text -

- Show quoted text -
Hi Mike,
2007 does open up the xls file for me, but it generates that warning I
displayed in my previous message, every single time. That is a bit
annoying and for a user can be even more so. Is there a way around
that warning?
Nov 20 '08 #5

"Doogie" <dn******@dtgnet.comwrote in message
news:b5**********************************@s20g2000 yqh.googlegroups.com...
On Nov 20, 1:51 am, "Mike Brind" <paxton...@hotmail.comwrote:
"Doogie" <dnlwh...@dtgnet.comwrote in message

news:8b**********************************@t2g2000y qm.googlegroups.com...
On Nov 19, 3:55 pm, "Anthony Jones" <AnthonyWJo...@yadayadayada.com>
wrote:


"Doogie" <dnlwh...@dtgnet.comwrote in message
news:e5**********************************@g38g2000 yqn.googlegroups.com...
Can anoyne tell me why this VBScript will create the file to Excel
just fine, but the Excel file will not open up? I am saving it as a
xlsx file instead of an xls one and I have the new version of Excel
on
my machine and have opened other Excel files with that extension but
this one I get the following error:
"Excel cannot open the file 'Test.xlsx' because the file format or
file extension is not valid. Verify that the file has not been
corrupted and the file extension matches the format of the file."
If I switch the file type to be .xls instead of .xlsx, it will save
the file and open with no problems. Below is an example of the VB
script I'm using.
dim Cn,Rs
set Cn=server.createobject("ADODB.connection")
set Rs=server.createobject("ADODB.recordset")
Cn.open "MyConnectionString"
Rs.open "mysqlquery",Cn,1,3
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment;
filename=Test.xlsx"
if Rs.eof <true then
response.write "<table border=1>"
while not Rs.eof
response.write "<tr><td>" & Rs.fields("mydatafield") & "</
td></tr>"
Rs.movenext
wend
response.write "</table>"
end if
set rs=nothing
Cn.close
Because an .xlsx file is opened strictly as a Zip package used by 2007
office applications. Whereas as an .xls file is opened by 2007 using the
same content sniffing tricks the previous versions did, hence it detects
whether the content is a proper ole storage based xls binary, a csv text
stream or a html text stream.
--
Anthony Jones - MVP ASP/ASP.NET- Hide quoted text -
- Show quoted text -
So is there anyway to get it to work in VB script or am I stuck using
xls? The reason I ask, is when opening up the xls file I always get a
warning message too. It still opens, but it's annoying to get the
message. The message I get is:
"The file you are trying to open, 'Test.xls' is in a different format
than specified by the file extension. Verify that the file is not
corrupted and is from a trusted source before opening the file. Do
you want to open the file now?"

I think that what Anthony was saying is that you are not actually creating
an xslx file. You are creating an html file, and giving it a different
extension. Previous versions of Excel were happy with this approach (to a
point), but the latest version appears not to like it at all. If you tried
to read from these "faux" Excel files using OleDb, they would also
complain
that the file format was not as expected.

One way to get round this is to create a real .xslx file in Excel and use
that as a blank template, then use the ACE OleDb provider to write your
data
to it in much the same way as if your were writing data to a database. The
ACE OleDb provider will need to be installed on the machine on which your
app is running for this to work.

Or you can ask yourself if you really need to try to generate the file
with
an xlsx extension, since 2007 will happily open xls files and even csv
files.

--
Mike Brind
MVP - ASP/ASP.NET- Hide quoted text -

- Show quoted text -
Hi Mike,
2007 does open up the xls file for me, but it generates that warning I
displayed in my previous message, every single time. That is a bit
annoying and for a user can be even more so. Is there a way around
that warning?
Only by preventing it from happening in the first place - generate a valid
file from within Excel, and use ADO to write to it.

--
Mike Brind
MVP - ASP/ASP.NET
Nov 20 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eliezer Figueroa | last post by:
Managing Multiple Excel incoming files? I have this situation. I have a client which have several locations they work primary with excel forms and they are thinking in doing reports with them....
5
by: jsudo | last post by:
I have a Access database with a large amount of records (close to 500,000) that I would like to export to Excel. I found out that Excel has the capability of of about 65,000 rows so I know I...
1
by: s_a_ravi | last post by:
hi, I want to export the data from datagrid and i dont want to wait upt the users to be prompted for save as dialog box to save the excel. i should saved in the client . Pls help me in that....
2
by: amitshinde02 | last post by:
Hi Experts, I have been struggling on this problem since last one month. I have a huge Excel sheet (Not well formatted) with around 10000 records or more. with around 60 fields. I want to...
0
by: genojoe | last post by:
I have a 4 column, 6 row Excel sheet with all cells formatted for text: 1 1 a a 2 2 b b 3 3 c c a 4 d 1 b 5 e 2 c 6 f 3 I have the following schema.ini:
2
by: Vmrincon | last post by:
Hi! I need to crete a visual basic .net application to read Excel 2000 files. Does anyone know if I need some kind of driver or some extra file to be able to do it? Thanks a lot!
1
by: Doogie | last post by:
Hi, I am writing a script to export data to an excel file which works fine but I've seen one thing that is confusing. If I use the extension .xlsx, the file will get created, but Excel won't let...
5
by: wolfjmt | last post by:
Hi, I am trying to write to data to a .xlsx file. I can currently write the data to a .xls file. I tried to change the extension of the xls file (to xlsx) but Excel gave the following error:Excel...
3
by: wolfjmt | last post by:
Hi, I am trying to write to data to a .xlsx file(Excel 2007). I can currently write the data to a .xls file. I tried to change the extension of the xls file (to xlsx) but Excel gave the following...
1
by: kstevens | last post by:
Does anyone know how to refer to a folder that is not "mine" on a remote terminal session. I know that when i export files to the "My Documents" folder (or delete files) that i use a blank...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.