`
webcode
  • 浏览: 5943212 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

C#.net 读写Excel

 
阅读更多

C#开发的WEB开发的简单框架 节省开发信息管理系统时间 提高开发效率
构建一个WEB开发的基础框架(主要包括数据库处理、页面框架及工具类),形成ASP.NET信息管理系统快速开发架构。
一、框架实现了基于XML定制的列表查询及图表展现
二、列表实现了复杂查询条件、合并行列(分组求和的处理)、汇总行及钻取数据和图表的钻取
三、编辑配置支持单表数据的增加和修改,针对开发人员实现的编辑页面可方便扩展主表新增字段
使开发人员专注于系统的业务实现,节省开发时间,提高开发效率

试用登录http://121.18.78.216/

ACE.OLEDB实现读取Excel数据

string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + fileName + ";Extended Properties='Excel "
+ (fileName.EndsWith("xls", StringComparison.CurrentCultureIgnoreCase) ? "8" : "12") + ".0;HDR=Yes'";
DataSet ds = new DataSet();
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
//返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等
DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });

//包含excel中表名的字符串数组
for (int k = 0; k < dtSheetName.Rows.Count; k++)
{
string strTableName = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
string str = "select * from [" + strTableName + "]";
OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
da.Fill(ds, strTableName.Replace("$", "").ToLower());
da.Dispose();
}
conn.Close();
conn.Dispose();
}
return ds;


XML方式读取:

/// <summary>
/// 获取Excel表格xml第index个Sheet的指定行列的数据
/// </summary>
/// <param name="doc">Excel的xml</param>
/// <param name="index">WorkSheet索引</param>
/// <param name="row">行索引</param>
/// <param name="col">列索引</param>
/// <returns></returns>
public static string GetCellData(XmlDocument doc, int index, int row, int col)
{
if (doc == null)
{
return null;
}
else
{
XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
xnm.AddNamespace(PREFIX, SSNS);
return XmlHelper.GetValue(doc.DocumentElement.ChildNodes[index]
.SelectNodes("Table/Row", xnm)[row]
.ChildNodes[col]
.ChildNodes[0]);
}
}
XML方式写Excel

/// <summary>
/// 获得Excel的标准xml
/// </summary>
/// <returns></returns>
public static XmlDocument GetExcel()
{
XmlDocument doc = new XmlDocument();
StringBuilder sbody = new StringBuilder();
sbody.Append("<?xml version=/"1.0/" encoding=/"utf-8/"?>");
sbody.Append("<?mso-application progid=/"Excel.Sheet/"?>");
sbody.Append("<Workbook xmlns=/"urn:schemas-microsoft-com:office:spreadsheet/"");
sbody.Append(" xmlns:o=/"urn:schemas-microsoft-com:office:office/"");
sbody.Append(" xmlns:x=/"urn:schemas-microsoft-com:office:excel/"");
sbody.Append(" xmlns:ss=/"urn:schemas-microsoft-com:office:spreadsheet/"");
sbody.Append(" xmlns:html=/"http://www.w3.org/TR/REC-html40/">");
sbody.Append("<DocumentProperties xmlns=/"urn:schemas-microsoft-com:office:office/">");
sbody.Append("<Author>贾世义</Author>");
sbody.Append("<LastAuthor>贾世义</LastAuthor>");
sbody.Append("<Created>" + DateTime.Today.ToString(Constants.DATE_FORMART) + "</Created>");
sbody.Append("</DocumentProperties>");
sbody.Append("<Styles>");
sbody.Append("<Style ss:ID=/"Default/" ss:Name=/"Normal/">");
sbody.Append("<Alignment ss:Horizontal=/"Left/" ss:Vertical=/"Center/"/>");
sbody.Append("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/"/>");
sbody.Append("<Interior/>");
sbody.Append("<NumberFormat/>");
sbody.Append("<Protection/>");
sbody.Append("</Style>");
sbody.Append("<Style ss:ID=/"sCenter/">");
sbody.Append("<Alignment ss:Horizontal=/"Center/" ss:Vertical=/"Center/"/>");
sbody.Append("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/"/>");
sbody.Append("</Style>");
sbody.Append("<Style ss:ID=/"sRight/">");
sbody.Append("<Alignment ss:Horizontal=/"Right/" ss:Vertical=/"Center/"/>");
sbody.Append("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/"/>");
sbody.Append("</Style>");
sbody.Append("<Style ss:ID=/"sBold/">");
sbody.Append("<Alignment ss:Horizontal=/"Left/" ss:Vertical=/"Center/"/>");
sbody.Append("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/" ss:Bold=/"1/"/>");
sbody.Append("</Style>");
sbody.Append("<Style ss:ID=/"sCenterBold/">");
sbody.Append("<Alignment ss:Horizontal=/"Center/" ss:Vertical=/"Center/"/>");
sbody.Append("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/" ss:Bold=/"1/" />");
sbody.Append("</Style>");
sbody.Append("<Style ss:ID=/"sDate/">");
sbody.Append("<Alignment ss:Horizontal=/"Left/" ss:Vertical=/"Center/"/>");
sbody.Append("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/"/>");
sbody.Append("<NumberFormat ss:Format=/"Short Date/"/>");
sbody.Append("</Style>");
sbody.Append("<Style ss:ID=/"sTime/">");
sbody.Append("<Alignment ss:Horizontal=/"Left/" ss:Vertical=/"Center/"/>");
sbody.Append("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/"/>");
sbody.Append("<NumberFormat ss:Format=/"yyyy/m/d// h:mm;@/"/>");
sbody.Append("</Style>");
sbody.Append("<Style ss:ID=/"sLink/" ss:Name=/"超链接/">");
sbody.Append("<Alignment ss:Horizontal=/"Left/" ss:Vertical=/"Center/"/>");
sbody.Append("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/" ss:Color=/"#0000FF/" ss:Underline=/"Single/"/>");
sbody.Append("</Style>");
sbody.Append("<Style ss:ID=/"sPercent/">");
sbody.Append("<Alignment ss:Horizontal=/"Right/" ss:Vertical=/"Center/"/>");
sbody.Append("<Font ss:FontName=/"宋体/" x:CharSet=/"134/" ss:Size=/"12/"/>");
sbody.Append("<NumberFormat ss:Format=/"Percent/"/>");
sbody.Append("</Style>");
sbody.Append("</Styles>");
sbody.Append("</Workbook>");
doc.LoadXml(sbody.ToString());
return doc;
}


源代码下载:http://download.csdn.net/source/3160593

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics