大家好 我想实现这样一个功能:
在一个form里面,点击按钮,选择一个excel,然后读取这个excel的值
应该如何做呢?
在一个form里面,点击按钮,选择一个excel,然后读取这个excel的值
应该如何做呢?
解决方案 »
- 水晶报表中子报表循环显示数据的一个问题
- 这个错误如何解决?找不到web.config文件,是要自己创建的吗?
- 关于DATAGIRD的刷新问题。
- 一个多维数据汇总```, 求一个算法, 进 ------ >>
- 请高手帮忙,session变量是否能在asp和asp.net中共享呢?
- 开发好的项目,要放到服务器上?我该怎么做,,,,需要复制哪些文件?服务器上只有framework
- 页面上的一个操作要想两个表插入数据,并且向第2个表插入数据的时候要用到第一个表的递增ID
- Fill: SelectCommand.Connection 属性尚未初始化。
- 我看DUWAMISH是可以看明白,但是要我做就无从下手了,烦!
- 如何使用DataSet保存两个数据库中的
- 网页里的中文一设置gb2312就乱码
- 后台弹出框
/// 读取复杂的对象
/// </summary>
/// <typeparam name="T">自定义存储数据的的对象</typeparam>
/// <param name="file">Excel数据路径</param>
/// <returns>返回T的List</returns>
public static List<T> ReadExcel<T>(string file)
{
List<T> list = new List<T>();
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties='Excel 8.0;IMEX=1'";
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connString))
{
using (System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", conn))
{
conn.Open();
DataSet ds = new DataSet();
myCommand.Fill(ds, "[Sheet1$]");
DataTable dt = ds.Tables[0]; Type type = typeof(T);
PropertyInfo[] infos = type.GetProperties();
Dictionary<string, string> dicProperties = new Dictionary<string, string>();
foreach (PropertyInfo info in infos)
{
object[] attributes = info.GetCustomAttributes(typeof(PropertyAttribute), true);
if (attributes != null && attributes.Length > 0)
{
PropertyAttribute pa = (PropertyAttribute)attributes[0];
dicProperties.Add(pa.Name, info.Name);
}
} foreach (DataRow row in dt.Rows)
{
T obj = (T)Activator.CreateInstance(typeof(T));
try
{
Fill<T>(row, dt.Columns, obj, dicProperties);
}
catch (Exception)
{
break;
}
list.Add(obj);
}
}
}
return list;
} private static void Fill<T>(DataRow row, DataColumnCollection columns, T t, Dictionary<string, string> dicFields)
{
foreach (DataColumn column in columns)
{
PropertyInfo info = t.GetType().GetProperty(dicFields[column.ColumnName]);
object val = Convert.ChangeType(row[column.ColumnName].ToString().Trim(), info.PropertyType);
info.SetValue(t, val, null);
}
} /// <summary>
/// 读取单列
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="file"></param>
/// <param name="columnName"></param>
/// <returns></returns>
public static List<T> ExcelRead<T>(string file, string columnName)
{
List<T> list = new List<T>();
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties='Excel 8.0;IMEX=1'";
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connString))
{
using (System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter("select " + columnName + " from [Sheet1$]", conn))
{
conn.Open();
DataSet ds = new DataSet();
myCommand.Fill(ds, "[Sheet1$]");
DataTable dt = ds.Tables[0];
foreach (DataRow row in dt.Rows)
{
object obj = row[columnName];
object o = Convert.ChangeType(obj, typeof(T));
if (o.ToString().Trim() == "")
{
break;
}
list.Add((T)o);
}
}
} return list;
}
using Microsoft.Office.Interop.Excel;private void btn_Import_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel Files|*.xls";
if (ofd.ShowDialog() == DialogResult.OK)
{
string filename = ofd.FileName;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook;
Microsoft.Office.Interop.Excel.Worksheet worksheet;
object oMissing = System.Reflection.Missing.Value;
workbook = excel.Workbooks.Open(filename, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
worksheet = (Worksheet)workbook.Worksheets[1];
int rowCount = worksheet.UsedRange.Rows.Count;
int colCount = worksheet.UsedRange.Columns.Count;
Microsoft.Office.Interop.Excel.Range range1;
int i;
for (i = 0; i < colCount; i++)
{
range1 = worksheet.Range[worksheet.Cells[1, i + 1], worksheet.Cells[1, i + 1]];
dt.Columns.Add(range1.Value2.ToString());
} int j;
for (j = 1; j < rowCount; j++)
{
DataRow dr = dt.NewRow();
for (i = 0; i < colCount; i++)
{
range1 = worksheet.Range[worksheet.Cells[j + 1, i + 1], worksheet.Cells[j + 1, i + 1]];
dr[i] = range1.Value2;
}
dt.Rows.Add(dr);
}
excel.Quit();
}
else
{
MessageBox.Show("文件路径出错!");
} //得到一个DataTable 再把DataTable 里面的数据取出来,赋值给文本框
}
还要添加 Microsoft.Office.Interop.Excel 的引用。