因为他们运行时容器是不同的,之间的缓存很难交互。
Archive for September 14th, 2008
C# 读取Excel数据
万无一失的办法是用Excel的COM控件
简单封装了一个类似SqlDataReader的类,注意要引用Excel的COM库,Microsoft Excel12.0 Object Library
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using ICMS.Ut.Utility;
namespace ICMS.Ut.FileUtility
{
/// <summary>
/// 模拟读取一个Excel文件
/// </summary>
public class ExcelDataReader : IDisposable
{
private Application app = null;
private Workbook wb = null;
private Worksheet sheet = null;
private string[] rowContents = null;
private int r = 0;
private int rowCnt = 0;
private int columnCnt = 0;
public int RowCnt
{
get { return rowCnt; }
}
public int ColumnCnt
{
get { return columnCnt; }
}
public string GetString(int k)
{
if (k >= columnCnt)
return null;
else
return rowContents[k];
}
public bool Read()
{
if (r >= rowCnt)
return false;
else
{
bool flag = false;
for (int i = 0; i < columnCnt; i++)
{
rowContents[i] = readContent(r, i);
if (!Checker.IsNullOrEmpty(rowContents[i]))
flag = true;
}
if (!flag)
{
rowCnt = r;
return false;
}
else
{
r++;
return true;
}
}
}
private string readContent(int r, int c)
{
Range range = (Range)(sheet.Cells[r + 1, c + 1]);
if (range.Value2 == null)
return null;
else
return range.Value2.ToString().Trim();
}
public ExcelDataReader(string filePath, IList<string> head)
{
app = new Application();
wb = app.Workbooks.Open(filePath, Type.Missing, true, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
sheet = (Worksheet)wb.Sheets[1];
rowContents = new string[sheet.Columns.Count];
rowCnt = sheet.Rows.Count;
columnCnt = sheet.Columns.Count;
for (int i = 0; i < sheet.Columns.Count; i++)
{
string v = readContent(0, i);
if (Checker.IsNullOrEmpty(v))
{
columnCnt = i;
break;
}
else head.Add(v);
}
r = 1;
}
#region IDisposable 成员
public void Dispose()
{
if (wb != null)
{
wb.Close(Type.Missing, Type.Missing, Type.Missing);
wb = null;
}
if (app != null)
{
app.Quit();
app = null;
}
}
#endregion
}
}
实习第六周
相对来说进度还算比较顺利,虽然不如预期,本周还是完成了学生信息管理部分的75%工作量,达到了最低限度的要求。
学 生基本信息管理的主要部分,学生信息管理做到了插件化可定制,一方面使用起来非常方便,另一方面降低了其他领域的工作量,很多功能不需要再编码只要在这里 定制一个模板即可。为了方便传递页面参数(主要是一些页面字段和操作方法),还封装了一个Session的参数管理器,支持使用idx来标识不同的页面, 从而有效的避免了Session的互相污染问题。至于Session空间,可以通过对Session参数管理器的定制(对idx容量的限制)而进行有效控 制。
在做这些东西中间,确实还是学到了很多知识,很多原来不关注的方面。其实ASP.net还是非常方便,原来不会用而已。
比如asp.net的gridview可以添加一个模板列(甚至可以可视化编辑),模板列可以添加任意控件,并通过一定的方式解析得到数据,用作自定义的id或者value。
<asp:TemplateField InsertVisible=”False”>
<HeaderTemplate>
<input id=”CheckAll” name = “CheckAll” type=”checkbox” onclick=”checkAll(this.checked)” />全选</HeaderTemplate>
<ItemTemplate>
<input id=”CheckOne” name =”CheckOne” type=”checkbox” onclick=”checkOne()”value=’<%# DataBinder.Eval(Container.DataItem, “StudentNo”)%>’/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=”StudentNo” HeaderText=”学号” SortExpression=”StudentNo” />
于是乎,通过GridView可以实现出任何复杂的表格结构用于显示大量数据。
比如原来很困扰的获取js使用server控件的id问题,其实也很简单,asp.net已经考虑了,只需要在js中写obj=document.getElementById(‘<%=province.ClientID %>’);即可。
这些总总都说明了原来我的一个论断,一个成熟的技术是有大部分问题的通用解决方法的,应该优先去查找这些方法,而不是去寻找很多自己想出来的变通的办法,这些办法可能带来的副作用更大。