/* EXPORTS DATA VIEWS LIST INTO EXCEL * modified from [http://www.codeproject.com/dotnet/ExportToExcel.asp] * * 1. source - is an array list containing multiple DataView that is filled with * data you want to save. You need to sort/filter this view before you pass * it to this function, because all it does is just saving it. * * * 2. Auto replaced string literals that needs escaping (XML character escapes) * as specified on [http://hdf.ncsa.uiuc.edu/HDF5/XML/xml_escape_chars.htm] * * 3. Multiple DataView will be saved into a single file as multiple 'tabs' * (multiple Worksheets in Excel). The name for the sheets will be based off * DataView.Table.TableName so prior to setting the DataView, set the name * of your DataTable. * * - zen sugiarto 6 May 2007 - */ public static void exportToExcel(ArrayList source, string fileName) { System.IO.StreamWriter excelDoc; excelDoc = new System.IO.StreamWriter(fileName); const string startExcelXML = "\r\n\r\n \r\n " + "\r\n " + "\r\n " + "\r\n \r\n " + "\r\n \r\n " + "\r\n "; const string endExcelXML = ""; int rowCount = 0; int sheetCount = 1; /* */ // write XML header excelDoc.Write(startExcelXML); DataView dv; for(int wscount=0; wscount < source.Count; wscount++ ) { dv = (DataView)source[wscount]; String tableName=dv.Table.TableName; if( tableName==null || tableName=="") tableName="Sheet"+wscount+1; excelDoc.Write(""); excelDoc.Write(""); excelDoc.Write(""); for(int x = 0; x < dv.Table.Columns.Count; x++) { excelDoc.Write(""); excelDoc.Write(dv.Table.Columns[x].ColumnName); excelDoc.Write(""); } excelDoc.Write(""); // writing each row IEnumerator iterator = dv.GetEnumerator(); //foreach(DataRow x in source.Tables[wscount].Rows) while( iterator.MoveNext() ) { DataRow x = ((DataRowView)iterator.Current).Row; rowCount++; //if the number of rows is > 64000 create a new page to continue output if(rowCount==64000) { rowCount = 0; sheetCount++; excelDoc.Write("
"); excelDoc.Write("
"); excelDoc.Write(""); excelDoc.Write(""); } excelDoc.Write(""); //ID=" + rowCount + " for(int y = 0; y < dv.Table.Columns.Count; y++) { System.Type rowType; rowType = x[y].GetType(); switch(rowType.ToString()) { case "System.String": string XMLstring = x[y].ToString(); XMLstring = XMLstring.Trim(); XMLstring = XMLstring.Replace("&","&"); XMLstring = XMLstring.Replace(">",">"); XMLstring = XMLstring.Replace("<","<"); XMLstring = XMLstring.Replace("\'","'"); XMLstring = XMLstring.Replace("\"","\\\""); XMLstring = XMLstring.Replace("\\","\\\\"); excelDoc.Write("" + ""); excelDoc.Write(XMLstring); excelDoc.Write(""); break; 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 DateTime XMLDate = (DateTime)x[y]; string XMLDatetoString = ""; //Excel Converted Date XMLDatetoString = XMLDate.Year.ToString() + "-" + (XMLDate.Month < 10 ? "0" + XMLDate.Month.ToString() : XMLDate.Month.ToString()) + "-" + (XMLDate.Day < 10 ? "0" + XMLDate.Day.ToString() : XMLDate.Day.ToString()) + "T" + (XMLDate.Hour < 10 ? "0" + XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) + ":" + (XMLDate.Minute < 10 ? "0" + XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) + ":" + (XMLDate.Second < 10 ? "0" + XMLDate.Second.ToString() : XMLDate.Second.ToString()) + ".000"; excelDoc.Write("" + ""); excelDoc.Write(XMLDatetoString); excelDoc.Write(""); break; case "System.Boolean": excelDoc.Write("" + ""); excelDoc.Write(x[y].ToString()); excelDoc.Write(""); break; case "System.Int16": case "System.Int32": case "System.Int64": case "System.Byte": excelDoc.Write("" + ""); excelDoc.Write(x[y].ToString()); excelDoc.Write(""); break; case "System.Decimal": case "System.Double": excelDoc.Write("" + ""); excelDoc.Write(x[y].ToString()); excelDoc.Write(""); break; case "System.DBNull": excelDoc.Write("" + ""); excelDoc.Write(""); excelDoc.Write(""); break; default: throw(new Exception(rowType.ToString() + " not handled.")); } } excelDoc.Write(""); } excelDoc.Write("
"); excelDoc.Write("
"); } excelDoc.Write(endExcelXML); excelDoc.Close(); }