I believe everyone Program When there is a place for interaction with Excel, I will use those operations of Microsoft Excel, sometimes, after the call, it cannot be released.
At the same time, I did not understand what others said on the Internet that they could export without using these DLL files. Recently, when I was working on a system, I happened to encounter someone else's machine that didn't have an Excel file. However, this is a headache.
Later, I thought about a compromise. Haha, since Excel is developed by Microsoft, and the lattice in Excel is also a table, haha, can I directly store a table as a file and save it as XLS.
Remember that each method has its own application scenarios ....
CodeVery simple
Public void exportselectdata (string filename)
{
Stringbuilder strbuilder = new stringbuilder ();
Strbuilder. append (@ "<! Doctype HTML public '-// W3C // dtd html 4.01 transitional // en'> ");
Strbuilder. append (@ "<HTML> ");
Strbuilder. append (@ "Strbuilder. append (@ "<title> document </title> ");
Strbuilder. append (@ "<meta HTTP-EQUIV = 'content-type' content = 'text/html; charset = UTF-8 '> ");
Strbuilder. append (@ "Strbuilder. append (@ "<body> ");
Strbuilder. append (@ "<Table Style = 'border-Right: Black thin solid; border-top: Black thin solid; border-left: Black thin solid; border-bottom: black thin solid '> ");
Strbuilder. append ("<tr> ");
Foreach (propertystyle style in mpropertystyles)
{
String STR = string. format (@ "<TD style = 'border-Right: Black thin solid; border-top: Black thin solid; border-left: Black thin solid; border-bottom: black thin solid; Background-color: lightgrey '> {0} </TD> ", convert. tostring (style. displayname ));
Strbuilder. append (STR );
}
Strbuilder. append ("</tr> ");
Foreach (alarmview in selectedalarmviews) (add your own data here ...)
{
If (alarmview! = NULL & alarmview. wrapper! = NULL & mpropertystyles! = NULL)
{
Strbuilder. append ("<tr> ");
Alarmwrapperpropertyhelper propertyhelper = new alarmwrapperpropertyhelper (alarmview. Wrapper );
Propertyhelper. nullvalue = NULL;
Foreach (propertystyle style in mpropertystyles)
{
Object value = propertyhelper. getvalue (style );
Strbuilder. append (htmltextserialize. Draw (value ));
}
Strbuilder. append ("</tr> ");
}
}
Strbuilder. append ("</table> ");
Strbuilder. append ("</body> ");
Strbuilder. append ("
Using (streamwriter write = new streamwriter (filename ))
{
Write. Write (strbuilder. tostring ());
Write. Close ();
}
}
It is mainly to spell a tabel. At the same time, this method can meet the needs of Excel export and export to HTML. In fact, we can save the file to HTML and then change the suffix. Of course, in this program, you only need to input a file path, regardless of XLS or HTML.
At the same time, this method is just a simple implementation of a single Excel worksheet. If the number of rows in the table exceeds 65535, the following data, I think, it cannot be displayed ......
This Excel export method does not test adding images or anything. However, I believe that images can also be exported. (For tests, images can be displayed on the local machine. If files are downloaded to other machines, it should not be able to display images, because, according to the program idea, it should be like this <TD> </img> </TD>, in this way, only the file connections are saved. To display images on other machines, you only need to organize the paths on other machines according to the 'file path, and save the image to this place .... there should be two ways to insert an image in Excel. One is to save the image connection, and the other is to include the image in an Excel file ......)