473,386 Members | 1,827 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,386 software developers and data experts.

C# Excel question

Hi

Does anyone know of a way (via code behind) to pull a single sheet out of a
Excel workbook and convert it to a stand alone html document?

Thanks

Brian
Mar 2 '07 #1
4 7339
"BrianDH" <Br*****@discussions.microsoft.comwrote in message
news:21**********************************@microsof t.com...
Does anyone know of a way (via code behind) to pull a single sheet out of
a
Excel workbook and convert it to a stand alone html document?
1) Use ADO.NET
http://www.google.co.uk/search?sourc...DO%2eNET+Excel

2) Use a 3rd-party tool:
http://www.aspose.com/Products/Aspos...s/Default.aspx

3) If the workbook is saved in the (relatively) new XML file format, use the
standard methods in the Xml namespace:
http://www.microsoft.com/downloads/d...displaylang=en
http://www.microsoft.com/downloads/d...displaylang=en

4) If this is a WinForms app and you know that Excel will be installed, use
InterOp
http://www.google.co.uk/search?sourc...=Excel+Interop

However, if this is a WebForms app, server-side Office Automation is not
recommended, and not actually supported by Microsoft:
http://support.microsoft.com/default...US;q257757#kb2
Mar 2 '07 #2
Hi Mark

Thanks for the links but I am still having an issue. The code does convert
the document to html, but it does all the sheets. I am trying to pull just
one sheet, save it to a new location so it can be viewed as a stand along
html doc.

Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook theWorkbook = excelApp.Workbooks._Open(excelFile, false,
false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Sheets sheets = theWorkbook.Worksheets;
object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml ;
String outputFile =
@"C:\SourceSafe\ConvertExcelSheetsToHTML\ReportFil es.html";
Microsoft.Office.Interop.Excel.Worksheet wsCurrent =
(Microsoft.Office.Interop.Excel.Worksheet)sheets.g et_Item(52);
wsCurrent.SaveAs(outputFile, format, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);

As you can see I am requesting sheet # 52 but it does all the sheets.

Thanks
"Mark Rae" wrote:
"BrianDH" <Br*****@discussions.microsoft.comwrote in message
news:21**********************************@microsof t.com...
Does anyone know of a way (via code behind) to pull a single sheet out of
a
Excel workbook and convert it to a stand alone html document?

1) Use ADO.NET
http://www.google.co.uk/search?sourc...DO%2eNET+Excel

2) Use a 3rd-party tool:
http://www.aspose.com/Products/Aspos...s/Default.aspx

3) If the workbook is saved in the (relatively) new XML file format, use the
standard methods in the Xml namespace:
http://www.microsoft.com/downloads/d...displaylang=en
http://www.microsoft.com/downloads/d...displaylang=en

4) If this is a WinForms app and you know that Excel will be installed, use
InterOp
http://www.google.co.uk/search?sourc...=Excel+Interop

However, if this is a WebForms app, server-side Office Automation is not
recommended, and not actually supported by Microsoft:
http://support.microsoft.com/default...US;q257757#kb2
Mar 2 '07 #3
"BrianDH" <Br*****@discussions.microsoft.comwrote in message
news:B5**********************************@microsof t.com...
The code does convert
the document to html, but it does all the sheets. I am trying to pull
just
one sheet, save it to a new location so it can be viewed as a stand along
html doc.

Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook theWorkbook = excelApp.Workbooks._Open(excelFile, false,
false, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Sheets sheets = theWorkbook.Worksheets;
object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml ;
String outputFile =
@"C:\SourceSafe\ConvertExcelSheetsToHTML\ReportFil es.html";
Microsoft.Office.Interop.Excel.Worksheet wsCurrent =
(Microsoft.Office.Interop.Excel.Worksheet)sheets.g et_Item(52);
wsCurrent.SaveAs(outputFile, format, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);

As you can see I am requesting sheet # 52 but it does all the sheets.
Hmm - I'm not exactly certain that you are...

It looks to me as if you are selecting sheet #52, but then calling the
SaveAs method - I'm pretty sure (but don't quote me on that) that the SaveAs
method applies to the entire workbook, not the currently selected worksheet,
even though you are invoking it from the Worksheet object...

Two thoughts spring to mind:

1) Delete all the other sheets, then invoke SaveAs...

2) Export the current sheet instead of invoking SaveAs...

Also, you may find that you get a better response by posting your message
in: microsoft.pulic.vsnet.vstools.office - that's the newsgroup
*specifically* for this type of question...
Mar 2 '07 #4
OK, I did it.
// Save Excel Docmnet as HTML
string excelFile = "@"C:\excelDocument.xls";
Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook excelWorkbook =
excelApp.Workbooks._Open(excelFile, false, false, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
Excel.Sheets excelSheet = excelWorkbook.Worksheets;
object sheetFormat =
Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml ;
String outputFile = @"C:\excelDocument.xls";
Microsoft.Office.Interop.Excel.Worksheet excelCurrentSheet =
(Microsoft.Office.Interop.Excel.Worksheet)excelShe et.get_Item(Convert.ToInt32(SheetNametextBox.Text) );
excelCurrentSheet.SaveAs(outputFile, sheetFormat, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
excelSheet = null;
excelCurrentSheet = null;
excelWorkbook = null;
excelApp.ActiveWorkbook.Close(false, excelFile, false);
if (excelApp != null)
{
excelApp.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComO bject(excelApp);
}
excelApp = null;
//////////////////////////// Read and collect html markup value from sheet
you need
WebRequest webRequest =
WebRequest.Create(@"C:\Inetpub\wwwroot\WebSites\sh eet057.htm");
StreamReader streamRequest = new
StreamReader(webRequest.GetResponse().GetResponseS tream());
System.Text.StringBuilder stringBuilderRequest = new
System.Text.StringBuilder();
string stringLine = "";
while ((stringLine = streamRequest.ReadLine()) != null)
{
if (stringLine.Length 0)
stringBuilderRequest.Append(stringLine);
}
streamRequest.Close();
pageValue = stringBuilderRequest.ToString();
CreateASCX();

////////////////////////////////// Create new page
private void CreateASCX()
{
// strip out reference to home page and tab page
int findStart = pageValue.IndexOf("</style>");
findStart = findStart + 8;
int findEnd = pageValue.IndexOf("</script>");
findEnd = findEnd - findStart;
pageValue = pageValue.Remove(findStart, findEnd);
StreamWriter SW;
SW = File.CreateText(@"C:\Inetpub\wwwroot\WebSites\newP age.ascx");
SW.WriteLine(pageValue.ToString());
SW.Close();
Application.Exit();
}
Brian

"Mark Rae" wrote:
"BrianDH" <Br*****@discussions.microsoft.comwrote in message
news:B5**********************************@microsof t.com...
The code does convert
the document to html, but it does all the sheets. I am trying to pull
just
one sheet, save it to a new location so it can be viewed as a stand along
html doc.

Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook theWorkbook = excelApp.Workbooks._Open(excelFile, false,
false, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Sheets sheets = theWorkbook.Worksheets;
object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml ;
String outputFile =
@"C:\SourceSafe\ConvertExcelSheetsToHTML\ReportFil es.html";
Microsoft.Office.Interop.Excel.Worksheet wsCurrent =
(Microsoft.Office.Interop.Excel.Worksheet)sheets.g et_Item(52);
wsCurrent.SaveAs(outputFile, format, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);

As you can see I am requesting sheet # 52 but it does all the sheets.

Hmm - I'm not exactly certain that you are...

It looks to me as if you are selecting sheet #52, but then calling the
SaveAs method - I'm pretty sure (but don't quote me on that) that the SaveAs
method applies to the entire workbook, not the currently selected worksheet,
even though you are invoking it from the Worksheet object...

Two thoughts spring to mind:

1) Delete all the other sheets, then invoke SaveAs...

2) Export the current sheet instead of invoking SaveAs...

Also, you may find that you get a better response by posting your message
in: microsoft.pulic.vsnet.vstools.office - that's the newsgroup
*specifically* for this type of question...
Mar 6 '07 #5

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

Similar topics

12
by: jimserac | last post by:
I had previously posted this in an Access forum with negative results so will try here. Although this question specifies an Access database, I also wish to accomplish this with a large MS SQL...
6
by: Sam Johnson | last post by:
HI I tried to send the following SQL string to an open databse, to export a table into excel format: g.Connection = conn 'valid OleDBConnection and Command objects g.CommandText = "SELECT *...
3
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue = ary(3, 1) 'xValue = C...
19
by: wreckingcru | last post by:
I'm trying to output a SQL query that is constructed thru my VB.net GUI into an excel file. Here is the code I'm using: 'Sqlstmt is the SQL query statement 'Conn is the SQL Connection object...
2
by: Mad Scientist Jr | last post by:
>From an asp.net web page I want the user to open the results of a SQL query in Excel, as automatically as possible (ie not having to loop through columns, rows, in code). For this,...
6
by: Gunawan | last post by:
Dear All, I have create an excel (COM Object) using this code Excel.Application xls = new Excel.Application(); but I can not remove it from memory although I have using close and quit ...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
7
by: ddecoste | last post by:
I have a need to add a visual representation to some data in Access. I need to draw a matix of squares inside another square. I have all the data that I need in a record in Access. The data...
3
by: pleaseexplaintome_2 | last post by:
using the code below (some parts not included), I create a new excel workbook with spreadheets. I then want to delete a spreadsheet, but a reference remains open and excel stays in task manager...
3
by: akristensen | last post by:
I am new to this site, so be patient if I do not ask the question correctly. Current Target Platform: Browser: MS IE, script language: Javascript (will use VBScript, but JS is preferred), External...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.