C# list导出excel文件
来源:互联网 发布:用u盘怎么重装mac系统 编辑:程序博客网 时间:2024/06/04 18:22
/// <summary>
/// 具体导出的方法
/// </summary>
/// <param name="listView">页面listview的ID</param>
/// <param name="strFileName">导出到的文件名</param>
public static void DoExport(ListView listView, string strFileName)
{
try
{
int rowNum = listView.Items.Count;
int columnNum = listView.Items[0].SubItems.Count;
int rowIndex = 1;
int columnIndex = 0;
if (rowNum == 0 || string.IsNullOrEmpty(strFileName))
{
return;
}
if (rowNum > 0)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook xlBook = excel.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.Worksheets[1];//取得sheet1
if (excel == null)
{
MessageUtil.ShowWarning("无法创建excel对象,可能您的系统没有安装excel!");
return;
}
excel.DefaultFilePath = "";
excel.DisplayAlerts = true;
excel.SheetsInNewWorkbook = 1;
//将ListView的列名导入Excel表第一行
foreach (ColumnHeader dc in listView.Columns)
{
columnIndex++;
excel.Cells[rowIndex, columnIndex] = dc.Text;
}
//将ListView中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
excel.Cells[rowIndex, columnIndex] = Convert.ToString(listView.Items[i].SubItems[j].Text);
}
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
//例外需要说明的是用strFileName,Excel.XlFileFormat.xlExcel9795保存方式时 当你的Excel版本不是95、97 而是2003、2007 时导出的时候会报一个错误:异常来自 HRESULT:0x800A03EC。 解决办法就是换成strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal。
xlBook.SaveAs(strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlBook = null;
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
MessageUtil.ShowTips("EXCEL文件导出成功!");
}
else
MessageUtil.ShowError("导出失败,没有可导出数据!");
}
catch (Exception ex)
{
MessageUtil.ShowException("导出文件时出错!\n", ex);
}
KillAllExcel();
}
private static void KillAllExcel()
{
List<Process> excelProcess = GetExcelProcesses();
for (int i = 0; i < excelProcess.Count; i++)
{
excelProcess[i].Kill();
}
}
/// <summary>
/// 获得所有的Excel进程
/// </summary>
/// <returns>所有的Excel进程</returns>
private static List<Process> GetExcelProcesses()
{
Process[] processes = Process.GetProcesses();
List<Process> excelProcesses = new List<Process>();
for (int i = 0; i < processes.Length; i++)
{
if (processes[i].ProcessName.ToUpper() == "EXCEL")
excelProcesses.Add(processes[i]);
}
return excelProcesses;
}
/// 具体导出的方法
/// </summary>
/// <param name="listView">页面listview的ID</param>
/// <param name="strFileName">导出到的文件名</param>
public static void DoExport(ListView listView, string strFileName)
{
try
{
int rowNum = listView.Items.Count;
int columnNum = listView.Items[0].SubItems.Count;
int rowIndex = 1;
int columnIndex = 0;
if (rowNum == 0 || string.IsNullOrEmpty(strFileName))
{
return;
}
if (rowNum > 0)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook xlBook = excel.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.Worksheets[1];//取得sheet1
if (excel == null)
{
MessageUtil.ShowWarning("无法创建excel对象,可能您的系统没有安装excel!");
return;
}
excel.DefaultFilePath = "";
excel.DisplayAlerts = true;
excel.SheetsInNewWorkbook = 1;
//将ListView的列名导入Excel表第一行
foreach (ColumnHeader dc in listView.Columns)
{
columnIndex++;
excel.Cells[rowIndex, columnIndex] = dc.Text;
}
//将ListView中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
excel.Cells[rowIndex, columnIndex] = Convert.ToString(listView.Items[i].SubItems[j].Text);
}
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
//例外需要说明的是用strFileName,Excel.XlFileFormat.xlExcel9795保存方式时 当你的Excel版本不是95、97 而是2003、2007 时导出的时候会报一个错误:异常来自 HRESULT:0x800A03EC。 解决办法就是换成strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal。
xlBook.SaveAs(strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlBook = null;
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
MessageUtil.ShowTips("EXCEL文件导出成功!");
}
else
MessageUtil.ShowError("导出失败,没有可导出数据!");
}
catch (Exception ex)
{
MessageUtil.ShowException("导出文件时出错!\n", ex);
}
KillAllExcel();
}
private static void KillAllExcel()
{
List<Process> excelProcess = GetExcelProcesses();
for (int i = 0; i < excelProcess.Count; i++)
{
excelProcess[i].Kill();
}
}
/// <summary>
/// 获得所有的Excel进程
/// </summary>
/// <returns>所有的Excel进程</returns>
private static List<Process> GetExcelProcesses()
{
Process[] processes = Process.GetProcesses();
List<Process> excelProcesses = new List<Process>();
for (int i = 0; i < processes.Length; i++)
{
if (processes[i].ProcessName.ToUpper() == "EXCEL")
excelProcesses.Add(processes[i]);
}
return excelProcesses;
}
0 0
- C# list导出excel文件
- C#导出Excel文件
- c# 导出Excel文件
- C#导出excel文件
- C# 导出EXCEL文件
- C#导出Excel文件
- C#导出excel文件
- C# dataGridView 导出Excel文件
- C# dateGRIDVIEW导出EXCEL文件
- c# listview导出excel文件
- C#导出泛型List到Excel
- 【.Net码农】【PropertyInfo】C# list导出Excel
- list集合导出Excel文件(一)
- list集合导出Excel文件(二)
- C#导出Excel|导出数据到Excel模板文件里
- Excel通用导出List<Object>对象到excel文件
- C#导出分Sheet的Excel文件
- c# aspx 导出Excel文件的方法
- hibernate中cascade和inverse
- python--简单三步,用 Python 发邮件
- jquery获取父窗口的元素
- iOS 有证书无密码 打包
- Firefox下“event is not defined”的解决方案
- C# list导出excel文件
- 模板方法模式(Template Method)
- 第3章 shell结构化命令------------(文件数据的循环与控制循环)
- Facebook 开源项目 Fresco 使用简介,facebookfresco
- [JQ权威指南]datepocker插件实现分段时间的选择
- mysql查询表所有字段的属性和注释
- jQuery封装的ajax——参数
- mapreduce中的shuffle
- Access键盘快捷键大全(五)