当前位置:文档之家› 将IList数据导出至Excel(亲测成功)

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

将IList数据导出至Excel(亲测成功)public static class ListExportToExcel<T> where T : class
{
public static Excel.Application m_xlApp = null;
private static string filePath = string.Empty;
public static void ExportExcel(List<T> list,string savepath,bool isbatch)
{
if (EmptyJudgment<T>.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<T> 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<T> where T : class
{
public static Excel.Application m_xlApp = null;
private static string filePath = string.Empty;
public static void ExportExcel(IList<T> list)
{
if (EmptyJudgment<T>.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<T> 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);
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();
MessM.PromptInfo("数据导出成功!");
}
catch (Exception ex)
{
MessM.PromptInfo("导出异常", "导出异常:" + ex.Message);
}
}
}。

相关主题