将实体对象集合数据写入Excel的通用类
2008-08-14 12:26
344 查看
好久没更新博客了!发一个实用的Excel工具类,用来将实体类集合写入到Excel,并且可以设定Excel中列规则,包括是否可以编辑,是否允许重复值。下面是完整代码,主函数里内容为使用说明。DataObject是演示用的一个测试实体类,ExcelWriter和PropertyInfo是主要的类。别忘了加上对Excel程序集的引用
public class ExcelReader
{
private Excel.Application application;
private Excel.Workbook workbook;
private Excel.Worksheet worksheet;
private object missing = Type.Missing;
private string filePath = "";
public ExcelReader(string filePath)
{
this.filePath = filePath;
}
/// <summary>
/// 验证文件隐藏Sheet中保存的文件批号是否和要求的一致
/// </summary>
/// <param name="rightSN"></param>
private void ValidateFileSN(string rightSN)
{
//读取隐藏Sheet中的文件批号
application = new Excel.Application();
workbook = application.Workbooks.Open(filePath, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
worksheet = workbook.Sheets.get_Item("HiddenSheet") as Excel.Worksheet;
Excel.Range firstRange = worksheet.get_Range("A1", "A1");
string fileSN = firstRange.get_Value(missing).ToString();
//退出并关闭Excel
workbook.Close(missing, missing, missing);
application.Quit();
ExcelWriter.KillExcelProcess(application);
//验证批号
if (fileSN != rightSN)
{
throw new Exception("文件批号不正确!");
}
}
/// <summary>
/// 从Excel中读取数据到DataTable如果rightSN为null则不检查文件批号
/// </summary>
/// <param name="rightSN"></param>
/// <returns></returns>
public DataTable ReadDataFromExcel(string rightSN)
{
//验证文件批号
if (rightSN != null)
{
ValidateFileSN(rightSN);
}
//读取Excel数据到DataTable
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(strConn);
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = new DataSet();
strExcel = "select * from [sheet1$]";
try
{
conn.Open();
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds, "dtSource");
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
conn.Close();
conn.Dispose();
}
}
}
public class ExcelReader
{
private Excel.Application application;
private Excel.Workbook workbook;
private Excel.Worksheet worksheet;
private object missing = Type.Missing;
private string filePath = "";
public ExcelReader(string filePath)
{
this.filePath = filePath;
}
/// <summary>
/// 验证文件隐藏Sheet中保存的文件批号是否和要求的一致
/// </summary>
/// <param name="rightSN"></param>
private void ValidateFileSN(string rightSN)
{
//读取隐藏Sheet中的文件批号
application = new Excel.Application();
workbook = application.Workbooks.Open(filePath, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
worksheet = workbook.Sheets.get_Item("HiddenSheet") as Excel.Worksheet;
Excel.Range firstRange = worksheet.get_Range("A1", "A1");
string fileSN = firstRange.get_Value(missing).ToString();
//退出并关闭Excel
workbook.Close(missing, missing, missing);
application.Quit();
ExcelWriter.KillExcelProcess(application);
//验证批号
if (fileSN != rightSN)
{
throw new Exception("文件批号不正确!");
}
}
/// <summary>
/// 从Excel中读取数据到DataTable如果rightSN为null则不检查文件批号
/// </summary>
/// <param name="rightSN"></param>
/// <returns></returns>
public DataTable ReadDataFromExcel(string rightSN)
{
//验证文件批号
if (rightSN != null)
{
ValidateFileSN(rightSN);
}
//读取Excel数据到DataTable
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(strConn);
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = new DataSet();
strExcel = "select * from [sheet1$]";
try
{
conn.Open();
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds, "dtSource");
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
conn.Close();
conn.Dispose();
}
}
}
相关文章推荐
- C#从实体对象集合中导出Excel的代码
- 通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法
- java easyreport 导入excel、 txt 数据关联集合置入对象(三)
- 根据EXCEL文件各数据表结构定义中创建ER图实体对象
- POI完美解析Excel数据到对象集合中(可用于将EXCEL数据导入到数据库)
- poi根据导入的excel数据自动封装成bean对象集合
- java:retainAll获取俩个自定义实体对象集合的重叠数据
- C#写入对象或集合类型数据到xml文件的方法
- 通过序列化和反序列化泛型数据实体集合来实现持久化数据对象的方法
- 生成二维码 加密解密类 TABLE转换成实体、TABLE转换成实体集合(可转换成对象和值类型) COOKIE帮助类 数据类型转换 截取字符串 根据IP获取地点 生成随机字符 UNIX时间转换为DATETIME\DATETIME转换为UNIXTIME 是否包含中文 生成秘钥方式之一 计算某一年 某一周 的起始时间和结束时间
- Java 对象集合数据导出到Excel
- tablib把数据导出为Excel、JSON、CSV等格式的Py库(写入数据并导出exl)
- excel在android读取,写入,更新数据
- 自定义可绑定数据的业务对象实体和强类型-Part Two
- c#导出Excel数据之常用对象
- 将Oracle数据库中的数据写入Excel
- python写入已存在的excel数据实例
- 使用VSIUAL C#.NET操作Excel -把DataTable中的数据写入Excel
- 使用VSIUAL C#.NET操作Excel -把DataTable中的数据写入Excel
- 从数据库读取数据写入excel并调用Winrar加密压缩输出