/// <summary>/// DataSet导出Excel/// </summary>/// <param name="arrTitle">列标题,若为null,则直接取dataset列标题</param>/// <param name="ds">要导出的DataSet</param>/// <param name="fileName">Excel文件名,不需要传入扩展名</param>protected void CreateExcel(string[] arrTitle, DataSet ds, string fileName){StringBuilder strb = new StringBuilder();strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\"");strb.Append(" <head> <meta http-equiv=’Content-Type' content=’text/html; charset=gb2312′>");strb.Append(" <style>");strb.Append(".xl26");strb.Append(" {mso-style-parent:style0;");strb.Append(" font-family:\"Times New Roman\", serif;");strb.Append(" mso-font-charset:0;");strb.Append(" mso-number-format:\"@\";}");strb.Append(" </style>");strb.Append(" <xml>");strb.Append(" <x:ExcelWorkbook>");strb.Append(" <x:ExcelWorksheets>");strb.Append(" <x:ExcelWorksheet>");strb.Append(" <x:Name>Sheet1 </x:Name>");strb.Append(" <x:WorksheetOptions>");strb.Append(" <x:DefaultRowHeight>285 </x:DefaultRowHeight>");strb.Append(" <x:Selected/>");strb.Append(" <x:Panes>");strb.Append(" <x:Pane>");strb.Append(" <x:Number>3 </x:Number>");strb.Append(" <x:ActiveCol>1 </x:ActiveCol>");strb.Append(" </x:Pane>");strb.Append(" </x:Panes>");strb.Append(" <x:ProtectContents>False </x:ProtectContents>");strb.Append(" <x:ProtectObjects>False </x:ProtectObjects>");strb.Append(" <x:ProtectScenarios>False </x:ProtectScenarios>");strb.Append(" </x:WorksheetOptions>");strb.Append(" </x:ExcelWorksheet>");strb.Append(" <x:WindowHeight>6750 </x:WindowHeight>");strb.Append(" <x:WindowWidth>10620 </x:WindowWidth>");strb.Append(" <x:WindowTopX>480 </x:WindowTopX>");strb.Append(" <x:WindowTopY>75 </x:WindowTopY>");strb.Append(" <x:ProtectStructure>False </x:ProtectStructure>");strb.Append(" <x:ProtectWindows>False </x:ProtectWindows>");strb.Append(" </x:ExcelWorkbook>");strb.Append(" </xml>");strb.Append("");strb.Append(" </head> <body> <table align=\"center\" style=’border-collapse:collapse;table-layout:fixed'> <tr>"); if (ds.Tables.Count > 0){//写列标题 if […]
View Detailsusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.OleDb;using System.Xml;using System.Xml.Xsl;using System.IO; namespace Amei.Data{ partial class DataSet { #region ExcelHelper #region 读取Excel public System.Data.DataSet ReadExcel(string path) { System.Data.DataSet ds = new System.Data.DataSet(); string extension = System.IO.Path.GetExtension(path.ToLower()); string connString = string.Empty; if (extension == ".xls") { connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; } else if (extension == ".xlsx") { connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; } string query = "SELECT * FROM [Sheet1$]"; try { OleDbConnection conn = new OleDbConnection(); OleDbCommand cmd […]
View Details