Opening multiple Excel templates from ASP.Nethttp://forums.asp.net/t/1449238.aspx/1?Opening+multiple+Excel+templates+from+ASP+NetSat, 03 Sep 2011 01:46:59 -040014492383303749http://forums.asp.net/p/1449238/3303749.aspx/1?Opening+multiple+Excel+templates+from+ASP+NetOpening multiple Excel templates from ASP.Net <p>Hey all!</p> <p>First off, I apologize if this is the incorrect place to put this question. I was unsure where to stick it and selected the clientside section because in all honesty, this is&nbsp;a problem that's located on the client's computer. :)</p> <p>I have a simple ASP.Net site&nbsp;with a bunch of&nbsp;LinkButtons that all point to the same&nbsp;XLT-file on the server. The LinkButtons, when clicked, set some values in a table in a database, and then they open the XLT-file by using Response.TransmitFile. See code below.&nbsp;The XLT-file has some VBA macros in it (that I didn't write) which contact&nbsp;the same&nbsp;database the&nbsp;LinkButtons wrote to in order to retrieve the name of the&nbsp;table in the link that was clicked.&nbsp;Then the macros&nbsp;fill the Excel workbook with values from this table.</p> <p>This is all fine and dandy, so far... The problem is when you attempt to open multiple instances, i.e. when you click on more links. The first time I click a link, I get the option to Open, Save or Cancel. I click Open, and Excel correctly opens, the macros kick in, and I see values. When I click a second link, a new window is supposed to open and values pertaining to the other link are supposed to display in it. Instead, the original&nbsp;Excel application is merely brought up and given focus, like if I'd clicked its button&nbsp;in the taskbar, and nothing happens. I see only the Workbook window with the values from the first link in it. I don't get a new option to Open, Save or Cancel, and I get no new Excel instance or Workbook window.</p> <p>Interestingly, this is an IE thing. It works perfectly well in FireFox. Here, it asks me if I want to open or save the file every time I click a link, and if I select Open, it simply gives me multiple instances of the entire&nbsp;Excel application. To begin with, I thought this might've been because all the links point to the same XLT-file and Windows somehow detects that the file is&nbsp;already open when I click the second link, but this isn't the case. I put in&nbsp;a button that opens an XLT-file with a different name just to test it, and it had the same behavior.<br> Anybody know how I can fix this? I've already tried a lot of the solutions involving the addition of &quot;%1&quot; to the File Type advanced settings as well as disabling DDE in order to force new, seperate applications to open, and none of them work.</p> <p>All help greatly appreciated!</p> <p>Christian Pedersen</p> <p>Code:</p> <pre class="prettyprint">void OpenLink_Click(object sender, EventArgs e) { // Recover tablename of the linkbutton that was clicked string tablename = ((LinkButton)sender).ID; *Do stuff here that writes data to the database that the Excel VBA Macros also access* // Transmit the file to the user. I tried to use Response.Redirect to begin with, but that allowed the users to open the file remotely, and then the macros didn't work, so I had to use TransmitFile in order to force a download: System.IO.FileInfo TanarosTemplateFile = new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory &#43; &quot;Tanaros.xlt&quot;); Response.Clear(); Response.AddHeader(&quot;Content-Length&quot;, TanarosTemplateFile.Length.ToString()); //Response.ContentType = &quot;application/vnd.ms-excel&quot;; Response.ContentType = &quot;application/msexcel&quot;; Response.AddHeader(&quot;Content-Disposition&quot;, &quot;filename=Tanaros.xlt&quot;); Response.TransmitFile(&quot;Tanaros.xlt&quot;); Response.End(); }</pre> <p><br> &nbsp;</p> 2009-07-20T13:13:02-04:003304030http://forums.asp.net/p/1449238/3304030.aspx/1?Re+Opening+multiple+Excel+templates+from+ASP+NetRe: Opening multiple Excel templates from ASP.Net <p>Here's a bit more information, if that helps:</p> <p>Changing the line,</p> <p><span>Response.AddHeader(<span>&quot;Content-Disposition&quot;</span><span>,&nbsp;</span><span>&quot;filename=Tanaros.xlt&quot;</span><span>);</span></span></p> <p><span><span>To:</span></span></p> <p><span>Response.AddHeader(<span>&quot;Content-Disposition&quot;</span><span>,&nbsp;</span><span>&quot;attachment; filename=Tanaros.xlt&quot;</span><span>);</span></span></p> <p><span><span>Reenables the links after the first download, i.e. I can click and open multiple excel windows, however, it's the actual, raw template file (.xlt) that I'm downloading and opening when I do it that way. Not the Excel sheet that the template file is supposed to &quot;turn into&quot; when it opens normally. The template file is read-only, its macros disabled. </span></span></p> <p><span><span>Any ideas?<br> </span></span></p> 2009-07-20T15:13:18-04:003304713http://forums.asp.net/p/1449238/3304713.aspx/1?Re+Opening+multiple+Excel+templates+from+ASP+NetRe: Opening multiple Excel templates from ASP.Net <p>&nbsp;This could be very unrelated, but if it helps... great</p> <p>ive noticed that while trying to open multiple windows, and using the window.open function, if the &quot;Name&quot; is the same for both windows, it will not bring up the new window.&nbsp; try setting a new name each time, even just adding a counter variable to the end of the page's name...</p> <p>its a long-shot, but sometimes the answer is suprisingly simple.</p> 2009-07-20T21:56:59-04:003316334http://forums.asp.net/p/1449238/3316334.aspx/1?Re+Opening+multiple+Excel+templates+from+ASP+NetRe: Opening multiple Excel templates from ASP.Net <p>I thought the same you did, but appending the filename header with the same filename (&quot;Tanaros.xlt&quot;) didn't cause a problem. Usually, it'll simply add a number in brackets to the opening file when given the filename of a window that's already open, i.e. if you click multiple times, you'd get Tanaros[1].xlt, Tanaros[2].xlt and so on.</p> <p>I finally managed to solve the issue described above, however, by appending the header,</p> <p>Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=Tanaros.xls&quot;);</p> <p>Before the call to Response.TransmitFile. This causes XP to open the <b>template-file</b> (XLT) as a <b>worksheet-file</b> (XLS), and then the macros run and I can open multiple windows.</p> <p>I now have another problem, though:</p> <p>If I attempt to re-invoke the macros in Excel, Excel tells me the &quot;File is locked for editing by [user-name]&quot;, where user-name is whoever's logged into the computer that opens the file. This, again, is a problem only in IE. FireFox doesn't have any of these issues whatsoever.<br> </p> 2009-07-28T10:51:39-04:003321435http://forums.asp.net/p/1449238/3321435.aspx/1?Re+Opening+multiple+Excel+templates+from+ASP+NetRe: Opening multiple Excel templates from ASP.Net <p>&nbsp;this (again) would be a suprisingly simple fix but being 20 yrs old and at my first programming job its all ive got for ya lol.</p> <p>&nbsp;</p> <p>maybe try excel.worksheet.close and then re-open the file to run the macros again?</p> 2009-07-30T13:11:10-04:003329023http://forums.asp.net/p/1449238/3329023.aspx/1?Re+Opening+multiple+Excel+templates+from+ASP+NetRe: Opening multiple Excel templates from ASP.Net <p></p> <blockquote><span class="icon-blockquote"></span> <h4>chickflickssuck</h4> <p></p> <p>&nbsp;this (again) would be a suprisingly simple fix but being 20 yrs old and at my first programming job its all ive got for ya lol.</p> <p></p> </blockquote> <p></p> <p>That's cool, any comment is greatly appreciated. I wish I'd known how to code when I was 20.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>chickflickssuck</h4> <p></p> <p>maybe try excel.worksheet.close and then re-open the file to run the macros again?</p> <p></p> </blockquote> <p></p> <p>You're fairly close to the solution I came up with, actually. The Excel namespace in which worksheet.close (among other things) reside are found either with VBA code in the macros or with ActiveX-objects instantiated through JavaScript. After much toiling with this bugger, I ended up detecting which browser was being used in the C# code-behind (using Request.Browser.Browser), and then handling IE differently from any other browser. If the user's using IE, I call a JavaScript function that uses ActiveX to force a whole new Excel application to open every time the user clicks a link. If the user's using Firefox (or whatever other browser) then the C# code-behind transmits the file like the code in the first post of this thread.</p> <p>I've tested this with the users and this seems to be the only working solution I can come up with. I wish IE wasn't this awfully cumbersome to work with. Just to add scorn to misery, this very post is being written in Firefox as we speak. I couldn't open the Reply-link in IE 8. It spawned Javascript exceptions when I tried.</p> <p>Case closed.</p> <p>/Christian H. Pedersen\<br> </p> 2009-08-04T13:33:34-04:004581596http://forums.asp.net/p/1449238/4581596.aspx/1?Re+Opening+multiple+Excel+templates+from+ASP+NetRe: Opening multiple Excel templates from ASP.Net <p></p> <blockquote><span class="icon-blockquote"></span> <h4>ChristianPedersen</h4> <p></p> <p>Hey all!</p> <p>First off, I apologize if this is the incorrect place to put this question. I was unsure where to stick it and selected the clientside section because in all honesty, this is&nbsp;a problem that's located on the client's computer. :)</p> <p>I have a simple ASP.Net site&nbsp;with a bunch of&nbsp;LinkButtons that all point to the same&nbsp;XLT-file on the server. The LinkButtons, when clicked, set some values in a table in a database, and then they open the XLT-file by using Response.TransmitFile. See code below.&nbsp;The XLT-file has some VBA macros in it (that I didn't write) which contact&nbsp;the same&nbsp;database the&nbsp;LinkButtons wrote to in order to retrieve the name of the&nbsp;table in the link that was clicked.&nbsp;Then the macros&nbsp;fill the Excel workbook with values from this table.</p> <p>This is all fine and dandy, so far... The problem is when you attempt to open multiple instances, i.e. when you click on more links. The first time I click a link, I get the option to Open, Save or Cancel. I click Open, and Excel correctly opens, the macros kick in, and I see values. When I click a second link, a new window is supposed to open and values pertaining to the other link are supposed to display in it. Instead, the original&nbsp;Excel application is merely brought up and given focus, like if I'd clicked its button&nbsp;in the taskbar, and nothing happens. I see only the Workbook window with the values from the first link in it. I don't get a new option to Open, Save or Cancel, and I get no new Excel instance or Workbook window.</p> <p>Interestingly, this is an IE thing. It works perfectly well in FireFox. Here, it asks me if I want to open or save the file every time I click a link, and if I select Open, it simply gives me multiple instances of the entire&nbsp;Excel application. To begin with, I thought this might've been because all the links point to the same XLT-file and Windows somehow detects that the file is&nbsp;already open when I click the second link, but this isn't the case. I put in&nbsp;a button that opens an XLT-file with a different name just to test it, and it had the same behavior.<br> Anybody know how I can fix this? I've already tried a lot of the solutions involving the addition of &quot;%1&quot; to the File Type advanced settings as well as disabling DDE in order to force new, seperate applications to open, and none of them work.</p> <p>All help greatly appreciated!</p> <p>Christian Pedersen</p> <p>Code:</p> <pre class="prettyprint">void OpenLink_Click(object sender, EventArgs e) { // Recover tablename of the linkbutton that was clicked string tablename = ((LinkButton)sender).ID; *Do stuff here that writes data to the database that the Excel VBA Macros also access* // Transmit the file to the user. I tried to use Response.Redirect to begin with, but that allowed the users to open the file remotely, and then the macros didn't work, so I had to use TransmitFile in order to force a download: System.IO.FileInfo TanarosTemplateFile = new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory &#43; &quot;Tanaros.xlt&quot;); Response.Clear(); Response.AddHeader(&quot;Content-Length&quot;, TanarosTemplateFile.Length.ToString()); //Response.ContentType = &quot;application/vnd.ms-excel&quot;; Response.ContentType = &quot;application/msexcel&quot;; Response.AddHeader(&quot;Content-Disposition&quot;, &quot;filename=Tanaros.xlt&quot;); Response.TransmitFile(&quot;Tanaros.xlt&quot;); Response.End(); }</pre> <p></p> </blockquote> <p></p> <p>Hey, i've just found this page while looking for how to open an excel template and add some data into it ,you said that it works for you, can you please send me the code you used?</p> <p>best regards!</p> 2011-09-03T01:46:59-04:00