These classes inherit from the abstract base class Stream, which supports reading and writing bytes into a file stream. You can also manually create Excel files, but the above functionality is what really impressed me.The StreamReader and StreamWriter classes are used for reading from and writing data to text files. Create the Excel worksheet from the data setĮ("MyExcelFile.xls", ds) Ĭreating the Excel file is as easy as that. OleDbDataAdapter adptr = new OleDbDataAdapter() OleDbCommand cmd = new OleDbCommand(sql, con) String sql = "SELECT Whatever FROM MyDBTable " Create a query and fill the data table with the data from the DB OleDbConnection con = new OleDbConnection(dbConnectionString) Open a DB connection (in this example with OleDB) Note that the ExcelLibrary code is the single line at the bottom: //Create the data set and tableĭataTable dt = new DataTable("New_DataTable") ĭs.Locale = .CurrentCulture ĭt.Locale = .CurrentCulture Here is an example taking data from a database and creating a workbook from it. Here are a couple links for quick reference: ExcelLibrary - GNU Lesser GPL EPPlus - GNU (LGPL) - No longer maintained EPPlus 5 - Polyform Noncommercial - Starting May 2020 NPOI - Apache License It seems to be more actively updated and documented as well.Īlso, as noted by below, EPPlus has support for Pivot Tables and ExcelLibrary may have some support ( Pivot table issue in ExcelLibrary) In all, EPPlus seems to be the best choice as time goes on. There are a few known bugs with each library as noted in the comments. You can also use EPPlus, which works only for Excel 2007/2010 format files (.xlsx files). Plus it has a DataSetHelper that lets you use DataSets and DataTables to easily work with Excel data.ĮxcelLibrary seems to still only work for the older Excel format (.xls files), but may be adding support in the future for newer 2007/2010 formats. xlsx format yet, but they are working on adding that functionality in. This looks to be a port of the PHP ExcelWriter that you mentioned above. It's a free, open source library posted on Google Code: You can use a library called ExcelLibrary. Obviously, you should use the same encoding when reading the file. TextWriter tw = new StreamWriter(filePath, false, isoLatin1Encoding) Īnd the file looks perfectly well. Sb.Append("dsfaskd jlsadfj laskjdflasjdf asdkfjalksjdf lkjdsfljas dddd jflasjdflkjasdlfkjasldfl asääääjdflkaslj d f") Įncoding isoLatin1Encoding = Encoding.GetEncoding("ISO-8859-1") To write true ISO-8859-1 use: Encoding isoLatin1Encoding = Encoding.GetEncoding("ISO-8859-1") ä,ö, ü) these characters will be written as a multibyte character. as long as the characters you are writing are present in ISO Latin-1 it will look like a ISO 8859-1 file, however as soon as you output a character that is not covered by ISO 8859-1 (e.g. Sadly, this is not possible, either you write UTF-8 or not. True means that the BOM should be emitted, using Encoding utf8WithoutBom = new UTF8Encoding(false) Well it writes the BOM because you are instructing it to, in the line Encoding utf8WithoutBom = new UTF8Encoding(true) Is this so hard to accomplish or I'm just getting a bad day?Īll help is greatly appreciated, thank you! My objective is create a file using UTF-8 as Encoding and 8859-1 as CharSet It's writing the BOM no matter what, and special chars (like Æ Ø Å) Response.AppendHeader("Content-Disposition", "attachment filename=" + filename + ".txt") Response.ContentType = "application/vnd.text" TextWriter tw = new StreamWriter(filePath, false, utf8WithoutBom) įoreach (string s in ()) pbs.GeneratedFile is a StringBuilder objectĮncoding utf8WithoutBom = new UTF8Encoding(true) String filePath = Path.Combine(Server.MapPath("App_Data"), filename + ".txt") String filename = String.Format("", dtFileCreated)
0 Comments
Leave a Reply. |