mysql list转table_将List中部分字段转换为DataTable中

由于原来方法导出数据量比较大 的时候,出现卡顿现象:搜索简单改造:(下面方法借助NPIO)

///

/// 将List中原文和译文转换为Datatable

///

/// 术语

/// 术语列宁表

/// 只包含原文和译文的DataTable

public System.Data.DataTable list2Datatable(List list)

{

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(MemoryFields));

System.Data.DataTable dt = new System.Data.DataTable();

//for (int i = 0; i < properties.Count; i++)

//{

PropertyDescriptor propertyOrig = properties["Orig"];//Orig为字段

dt.Columns.Add("原文", propertyOrig.PropertyType);

PropertyDescriptor propertyTran = properties["Tran"]; //Tran为字段

dt.Columns.Add("译文", propertyTran.PropertyType);

//}

object[] values = new object[2];

foreach (MemoryFields item in list)

{

//for (int i = 0; i < values.Length; i++)

//{

values[0] = properties["Orig"].GetValue(item);

values[1] = properties["Tran"].GetValue(item);

//}

dt.Rows.Add(values);

}

return dt;

}

private string filename = "";

private IWorkbook workbook = null;

private FileStream fs = null;

private bool disposed;

///

/// 到处到Excel

///

/// 导出文件名

/// table

/// sheetName

///

///

public int DataTableToExcel(string FileName, System.Data.DataTable data, string sheetName, bool isColumnWritten)

{

filename = FileName;

int i = 0;

int j = 0;

int count = 0;

ISheet sheet = null;

fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

if (filename.IndexOf(".xlsx") > 0) // 2007版本

workbook = new XSSFWorkbook();

else if (filename.IndexOf(".xls") > 0) // 2003版本

workbook = new HSSFWorkbook();

try

{

if (workbook != null)

{

sheet = workbook.CreateSheet(sheetName);

}

else

{

return -1;

}

if (isColumnWritten == true) //写入DataTable的列名

{

IRow row = sheet.CreateRow(0);

for (j = 0; j < data.Columns.Count; ++j)

{

row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);

}

count = 1;

}

else

{

count = 0;

}

for (i = 0; i < data.Rows.Count; ++i)

{

IRow row = sheet.CreateRow(count);

for (j = 0; j < data.Columns.Count; ++j)

{

row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());

}

++count;

}

workbook.Write(fs); //写入到excel

return count;

}

catch (Exception ex)

{

Console.WriteLine("Exception: " + ex.Message);

return -1;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值