将IList数据导出至Excel(亲测成功)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

将IList数据导出至Excel(亲测成功)public static class ListExportToExcel where T : class

{

public static Excel.Application m_xlApp = null;

private static string filePath = string.Empty;

public static void ExportExcel(List list,string savepath,bool isbatch)

{

if (EmptyJudgment.IListIsData(list))

{

if (ValueHelp.IsNullOrEmptyBool(savepath))

{

SaveFileDialog s = new SaveFileDialog

{

Title = "保存Excel文件",

Filter = "Excel文件(*.xls)|*.xls",

FilterIndex = 1

};

if (s.ShowDialog() == DialogResult.OK)

filePath = s.FileName;

else

return;

}

else

{

filePath = savepath;

}

ListToExcel(list,isbatch);

}

else

{

MessM.PromptInfo("无数据可导出!");

return;

}

}

private static void ListToExcel(List list,bool IsBatch)

{

Excel.Application m_xlApp = new Excel.Application

{

DisplayAlerts = false,//不显示更改提示

Visible = false

};

Excel.Workbooks workbooks = m_xlApp.Workbooks;

Excel.Workbook workbook =

workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

try

{

object Nothing = Missing.Value;

object format = XlFileFormat.xlWorkbookDefault;

PropertyInfo[] GetPropertyInfo =

typeof(T).GetProperties(System.Reflection.BindingFlags.Public |

System.Reflection.BindingFlags.Instance);

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

{

for (int j = 0; j < GetPropertyInfo.Length; j++)

{

PropertyInfo pi = GetPropertyInfo[j];

if (i == 0)

{

m_xlApp.Cells[1, j + 1] = "'" + ;//字段名称

m_xlApp.Cells[2, j + 1] = "'" + pi.GetValue(list[0], null);

}

else

{

m_xlApp.Cells[i + 2, j + 1] = "'" + pi.GetValue(list[i], null);

}

}

}

workbook.SaveAs(filePath, format, Nothing, Nothing, Nothing, Nothing, XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);

workbook.Close(Nothing, Nothing, Nothing);

m_xlApp.Quit();

if (!IsBatch)

{

MessM.PromptInfo("数据导出成功!");

}

}

catch (Exception ex)

{

MessM.PromptInfo("导出异常", "导出异常:" + ex.Message);

}

}

}

public static class IListExportToExcel where T : class

{

public static Excel.Application m_xlApp = null;

private static string filePath = string.Empty;

public static void ExportExcel(IList list)

{

if (EmptyJudgment.IListIsData(list))

{

SaveFileDialog s = new SaveFileDialog

{

Title = "保存Excel文件",

Filter = "Excel文件(*.xls)|*.xls",

FilterIndex = 1

};

if (s.ShowDialog() == DialogResult.OK)

filePath = s.FileName;

else

return;

ListToExcel(list);

}

else

{

MessM.PromptInfo("无数据可导出!");

return;

}

}

private static void ListToExcel(IList list)

{

Excel.Application m_xlApp = new Excel.Application

{

DisplayAlerts = false,//不显示更改提示

Visible = false

};

Excel.Workbooks workbooks = m_xlApp.Workbooks;

Excel.Workbook workbook =

workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

try

{

object Nothing = Missing.Value;

object format = XlFileFormat.xlWorkbookDefault;

PropertyInfo[] GetPropertyInfo =

typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

相关文档
最新文档