|
...
All,
This is just FYI.
The XML directive and xmlns spacing both need this simple fix.
See code fix below, for the startExcelXML constant, etc.
<pre lang="c#">
const string startExcelXML = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<Workbook " +
"xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" +
" xmlns =\"urn:schemas-microsoft-com:office:office\"\r\n " +
"xmlns:x=\"urn:schemas-microsoft-com:office:" +
"excel\"\r\n xmlns:ss=\"urn:schemas-microsoft-com:" +
"office:spreadsheet\">\r\n <Styles>\r\n " +
"<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " +
"<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" +
"\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" +
"\r\n <Protection/>\r\n </Style>\r\n " +
"<Style ss:ID=\"BoldColumn\">\r\n <Font " +
"x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " +
"<Style ss:ID=\"StringLiteral\">\r\n <NumberFormat" +
" ss:Format=\"@\"/>\r\n </Style>\r\n <Style " +
"ss:ID=\"Decimal\">\r\n <NumberFormat " +
"ss:Format=\"0.0000\"/>\r\n </Style>\r\n " +
"<Style ss:ID=\"Integer\">\r\n <NumberFormat " +
"ss:Format=\"0\"/>\r\n </Style>\r\n <Style " +
"ss:ID=\"DateLiteral\">\r\n <NumberFormat " +
"ss:Format=\"mm/dd/yyyy;@\"/>\r\n </Style>\r\n " +
"</Styles>\r\n ";
</pre>
HTH.
Thanks.
-- Mark Kamoski
...
|
|
|
|
|
...
FYI,
When opening the resulting XML file, Excel will complain...
The file format and extension of 'test.xls' don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?
...but it still works.
Just FYI.
HTH.
Thanks.
-- Mark Kamoski
...
|
|
|
|
|
My vote of 5. Nice and Simple. Thanks.
|
|
|
|
|
What should be defined for the xml schema to format 1000 grouping symbol decimal number. For examaple, the number 1.234,56 have the decimal symbol as ',' and 1000 grouping symbol as '.'. The sample code above just define the decimal symbol only. Thanks in advance and looking for any response.
|
|
|
|
|
Hello, is there anyone can help me this matter???? please!!!!
|
|
|
|
|
Very nicely done and very helpful post.
|
|
|
|
|
This works perfectly when I am testing in Visual Studio, however when I deploy to my production server (Win 2008 R2 Standard, ASP.NET 4.0) and click Open at the IE prompt I get the error message:
-------------------------------
Microsoft Office Excel cannot open or save any more documents because there is not enough available memory or disk space.
• To make more memory available, close workbooks or programs you no longer need.
• To free disk space, delete files you no longer need from the disk you are saving to.
-------------------------------
Any help would be greatly appreciated.
Many thanks
Paul
|
|
|
|
|
Have a read of this:
It's a completely free library, lets you export a DataSet (or DataTable or List<>) into a "genuine" Excel 2007 .xlsx file.
http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm
All of the source code is given, free of charge, aswell as a demo application.
It's very easy to add to your own applications, you just need to call one function, passing in an Excel filename, and your data source:
DataSet ds = CreateSampleData();
string excelFilename = "C:\\Sample.xlsx";
CreateExcelFile.CreateExcelDocument(ds, excelFilename);
It doesn't get much simpler than that....
|
|
|
|
|
|
It should absolutely NOT be this hard to export from C# (native, paid-for, proprietary MS product) to Excel (native, paid-for, proprietary MS product). Don't they sell us on using this crap because it's "intergrated"??
In any case, it works great. The only thing that came up in my usage was that it doesn't have a case handler for Byte[] arrays. Thankfully, I don't care about that, so I just added this bit of code and it worked like a charm:
case "System.Byte[]":
break;
|
|
|
|
|
This piece "urn:schemas- microsoft-com:office" needs to be "urn:schemas-microsoft-com:office" Also remove the xml version tag at the top and it should works great.
|
|
|
|
|
|
Your code above for `System.DateTime` is certainly not intuitive.
This "one liner" with accompanying explanation comment is faster and easier:
case "System.DateTime":
excelDoc.Write(string.Format("<Cell ss:StyleID=\"DateLiteral\"><Data ss:Type=\"DateTime\">{0:s}</Data></Cell>", x[y]));
break;
Reference: String Format for DateTime [C#][^]
|
|
|
|
|
|
Sorry for my ingnorance here but I would like to connect this method with a button click event,,,how will I solve that?
Kindly
Simon
|
|
|
|
|
This article was almost the answer to my prayers. Unfortunately, by company rules, I am bound to VB and all text files, including XML files, must be "properly formatted."
Here is my VB version of your code:
Module ExportToExcel
Private Const startExcelXML As String = _
"<xml version> " & vbCrLf & _
"<Workbook " & vbCrLf & _
" xmlns=""urn:schemas-microsoft-com:office:spreadsheet"" " & vbCrLf & _
" xmlns:o=""urn:schemas-microsoft-com:office:office"" " & vbCrLf & _
" xmlns:x=""urn:schemas-microsoft-com:office:excel"" " & vbCrLf & _
" xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"" > " & vbCrLf
Private Const stylesXML As String = _
vbTab & "<Styles>" & vbCrLf & _
vbTab & vbTab & "<Style ss:ID=""Default"" ss:Name=""Normal"">" & vbCrLf & _
vbTab & vbTab & vbTab & "<Alignment ss:Vertical=""Bottom""/>" & vbCrLf & _
vbTab & vbTab & vbTab & "<Borders/>" & vbCrLf & _
vbTab & vbTab & vbTab & "<Font/>" & vbCrLf & _
vbTab & vbTab & vbTab & "<Interior/>" & vbCrLf & _
vbTab & vbTab & vbTab & "<NumberFormat/>" & vbCrLf & _
vbTab & vbTab & vbTab & "<Protection/>" & vbCrLf & _
vbTab & vbTab & "</Style>" & vbCrLf & _
vbTab & vbTab & "<Style ss:ID=""BoldColumn"">" & vbCrLf & _
vbTab & vbTab & vbTab & "<Font x:Family=""Swiss"" ss:Bold=""1""/>" & vbCrLf & _
vbTab & vbTab & "</Style>" & vbCrLf & _
vbTab & vbTab & "<Style ss:ID=""StringLiteral"">" & vbCrLf & _
vbTab & vbTab & vbTab & "<NumberFormat ss:Format=""@""/>" & vbCrLf & _
vbTab & vbTab & "</Style>" & vbCrLf & _
vbTab & vbTab & "<Style ss:ID=""Decimal"">" & vbCrLf & _
vbTab & vbTab & vbTab & "<NumberFormat ss:Format=""0.0000""/>" & vbCrLf & _
vbTab & vbTab & "</Style>" & vbCrLf & _
vbTab & vbTab & "<Style ss:ID=""Integer"">" & vbCrLf & _
vbTab & vbTab & vbTab & "<NumberFormat ss:Format=""0""/>" & vbCrLf & _
vbTab & vbTab & "</Style>" & vbCrLf & _
vbTab & vbTab & "<Style ss:ID=""DateLiteral"">" & vbCrLf & _
vbTab & vbTab & vbTab & "<NumberFormat ss:Format=""mm/dd/yyyy;@""/>" & vbCrLf & _
vbTab & vbTab & "</Style>" & vbCrLf & _
vbTab & vbTab & "<Style ss:ID=""DateSpecial"">" & vbCrLf & _
vbTab & vbTab & vbTab & "<NumberFormat ss:Format=""yyyy-MM""/>" & vbCrLf & _
vbTab & vbTab & "</Style>" & vbCrLf & _
vbTab & "</Styles>" & vbCrLf
Private Const endExcelXML As String = "</Workbook>" & vbCrLf
Public Sub ExportToExcel(ByVal source As DataSet, ByVal fileName As String)
Dim excelDoc As System.IO.StreamWriter
excelDoc = New System.IO.StreamWriter(fileName)
Dim rowCount As Integer = 0
Dim sheetCount As Integer = 1
' /*
' <xml version>
' <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
'xmlns: o = "urn:schemas-microsoft-com:office:office"
'xmlns: x = "urn:schemas-microsoft-com:office:excel"
' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
' <Styles>
' <Style ss:ID="Default" ss:Name="Normal">
' <Alignment ss:Vertical="Bottom"/>
' <Borders/>
' <Font/>
' <Interior/>
' <NumberFormat/>
' <Protection/>
' </Style>
' <Style ss:ID="BoldColumn">
' <Font x:Family="Swiss" ss:Bold="1"/>
' </Style>
' <Style ss:ID="StringLiteral">
' <NumberFormat ss:Format="@"/>
' </Style>
' <Style ss:ID="Decimal">
' <NumberFormat ss:Format="0.0000"/>
' </Style>
' <Style ss:ID="Integer">
' <NumberFormat ss:Format="0"/>
' </Style>
' <Style ss:ID="DateLiteral">
' <NumberFormat ss:Format="mm/dd/yyyy;@"/>
' </Style>
' </Styles>
' <Worksheet ss:Name="Sheet1">
' </Worksheet>
' </Workbook>
' */
excelDoc.Write(startExcelXML)
excelDoc.Write(stylesXML)
excelDoc.WriteLine(vbTab & "<Worksheet ss:Name=""Sheet" + _
sheetCount.ToString + """>")
excelDoc.WriteLine(vbTab & vbTab & "<Table>")
excelDoc.WriteLine(vbTab & vbTab & vbTab & "<Row>")
For x As Integer = 0 To source.Tables(0).Columns.Count - 1
excelDoc.WriteLine(vbTab & vbTab & vbTab & vbTab & _
"<Cell ss:StyleID=""BoldColumn"">")
excelDoc.Write(vbTab & vbTab & vbTab & vbTab & vbTab)
excelDoc.Write("<Data ss:Type=""String"">")
excelDoc.Write(source.Tables(0).Columns(x).ColumnName)
excelDoc.Write("</Data>" & vbCrLf)
excelDoc.WriteLine(vbTab & vbTab & vbTab & vbTab & "</Cell>")
Next x
excelDoc.WriteLine(vbTab & vbTab & vbTab & vbTab & "</Row>")
For Each x As DataRow In source.Tables(0).Rows
rowCount += 1
'if the number of rows is > 64000 create a new page to continue output
If (rowCount = 64000) Then
rowCount = 0
sheetCount += 1
excelDoc.Write("</Table>")
excelDoc.Write("</Worksheet>")
excelDoc.Write( _
"<Worksheet ss:Name=""Sheet" + sheetCount.ToString + """>")
excelDoc.Write("<Table>")
End If
excelDoc.WriteLine(vbTab & vbTab & vbTab & "<Row>")
For y As Integer = 0 To source.Tables(0).Columns.Count - 1
Dim rowType As System.Type
rowType = x(y).GetType()
Select Case (rowType.ToString())
Case "System.String"
Dim XMLstring As String = x(y).ToString()
XMLstring = XMLstring.Trim()
XMLstring = XMLstring.Replace("&", "&")
XMLstring = XMLstring.Replace(">", ">")
XMLstring = XMLstring.Replace("<", "<")
excelDoc.WriteLine(vbTab & vbTab & vbTab & vbTab & _
"<Cell ss:StyleID=""StringLiteral"">")
excelDoc.Write(vbTab & vbTab & vbTab & vbTab & vbTab)
excelDoc.Write("<Data ss:Type=""String"">")
excelDoc.Write(XMLstring)
Case "System.DateTime"
' Excel has a specific Date Format of YYYY-MM-DD followed by
' the letter 'T' then hh:mm:sss.lll
' Example 2005-01-31T24:01:21.000
' The Following Code puts the date stored in XMLDate
' to the format above
Dim XMLDate As DateTime = CDate(x(y))
Dim XMLDatetoString As String = "" ' Excel Converted Date
XMLDatetoString = XMLDate.Year.ToString() & _
"-" & _
IIf(XMLDate.Month < 10, _
"0" & XMLDate.Month.ToString(), _
XMLDate.Month.ToString()).ToString & _
"-" & _
IIf(XMLDate.Day < 10, _
"0" & XMLDate.Day.ToString(), _
XMLDate.Day.ToString()).ToString & _
"T" & _
IIf(XMLDate.Hour < 10, _
"0" & XMLDate.Hour.ToString(), _
XMLDate.Hour.ToString()).ToString & _
":" & _
IIf(XMLDate.Minute < 10, _
"0" & XMLDate.Minute.ToString(), _
XMLDate.Minute.ToString()).ToString & _
":" & _
IIf(XMLDate.Second < 10, _
"0" & XMLDate.Second.ToString(), _
XMLDate.Second.ToString()).ToString & _
".000"
excelDoc.WriteLine(vbTab & vbTab & vbTab & vbTab & _
"<Cell ss:StyleID=""DateSpecial"">")
excelDoc.Write(vbTab & vbTab & vbTab & vbTab & vbTab)
excelDoc.Write("<Data ss:Type=""DateTime"">")
excelDoc.Write(XMLDatetoString)
Case "System.Boolean"
excelDoc.WriteLine(vbTab & vbTab & vbTab & vbTab & _
"<Cell ss:StyleID=""StringLiteral"">")
excelDoc.Write(vbTab & vbTab & vbTab & vbTab & vbTab)
excelDoc.Write("<Data ss:Type=""String"">")
excelDoc.Write(x(y).ToString())
Case "System.Int16", "System.Int32", "System.Int64", _
"System.Byte"
excelDoc.WriteLine(vbTab & vbTab & vbTab & vbTab & _
"<Cell ss:StyleID=""Integer"">")
excelDoc.Write(vbTab & vbTab & vbTab & vbTab & vbTab)
excelDoc.Write("<Data ss:Type=""Number"">")
excelDoc.Write(x(y).ToString())
Case "System.Decimal", "System.Double"
excelDoc.WriteLine(vbTab & vbTab & vbTab & vbTab & _
"<Cell ss:StyleID=""Decimal"">")
excelDoc.Write(vbTab & vbTab & vbTab & vbTab & vbTab)
excelDoc.Write("<Data ss:Type=""Number"">")
excelDoc.Write(x(y).ToString())
Case "System.DBNull"
excelDoc.WriteLine(vbTab & vbTab & vbTab & vbTab & _
"<Cell ss:StyleID=""StringLiteral"">")
excelDoc.Write(vbTab & vbTab & vbTab & vbTab & vbTab)
excelDoc.Write("<Data ss:Type=""String"">")
excelDoc.Write("")
Case Else
Throw (New Exception(rowType.ToString() + " not handled."))
End Select
excelDoc.Write("</Data>" & vbCrLf)
excelDoc.WriteLine(vbTab & vbTab & vbTab & vbTab & "</Cell>")
Next y
excelDoc.WriteLine(vbTab & vbTab & vbTab & "</Row>")
Next x
excelDoc.WriteLine(vbTab & vbTab & "</Table>")
excelDoc.WriteLine(vbTab & "</Worksheet>")
excelDoc.Write(endExcelXML)
excelDoc.Close()
End Sub ' ExportToExcel
End Module ' ExportToExcel
|
|
|
|
|
When applying this approach on an ASP.Net web page, to support concurrent operations, it is better to export to a new excel file each time. However, after the user opened and viewed this xls file, the excel file will stay on the server. Is it possible to avoid saving xls files on the web server, or delete them after requests end?
|
|
|
|
|
Hi all,
When i am trying to open excel in office 2007, it is giving XML parser error. But it is working fine in office 2003.
I am not able to find the root cause of this problem. Any help regarding this, will be really appreciated.
Thanks in advance.
Kind regards,
Raghav
|
|
|
|
|
The excel shows up as blank when opened when i am using response.contenttype. Actually i want the user to have open/save option.
Any help on this will be highly appreciated. Thanks in Advance
|
|
|
|
|
thanks... Xodiak![Thumbs Up | :thumbsup:](https://cdn.statically.io/img/codeproject.global.ssl.fastly.net/script/Forums/Images/thumbs_up.gif)
|
|
|
|
|
I can't speak English well.
Please note that...
how to convert tag from dataset to excel sheet like a excel ALT+Enter function key ?
|
|
|
|
|
|
Just a quick question, where did you find the information regarding the xml schema information? I see some of the items I want to modify but need the source so I do not cause any other problems.
Thanks
|
|
|
|
|
1. Open excel
2. Preform any formatting you want to do to an excel cell
3. File -> Save As xml document
4. Open in notepad to see formatting styles that were exported with the XML document.
|
|
|
|
|
Hello sir,
I am developing a application in vb.net sir. My requirement is Export dataset(More than 1 lakh records)to
Excel 2007 with hyperllink in one column.
Thanx
Mohan Goud
|
|
|
|