zoukankan      html  css  js  c++  java
  • ExcelHelper导出DataTable到Excel文档

     1using System;
     2using System.IO;
     3using System.Data;
     4using System.Web;
     5using System.Text;
     6using System.Globalization;
     7using System.Collections;
     8using System.Windows.Forms;
     9using Microsoft.CSharp;
    10
    11namespace Com.Elong.BusinessService.Domain.CommissionAccount
    12{
    13    public class ExcelHelper
    14    {
    15        public static void ExportToExcel(string filePath, DataSet ds)
    16        {
    17            object oMissing = System.Reflection.Missing.Value;
    18            Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
    19            try
    20            {
    21                // 打开Excel文件。以下为Office 2000.
    22                Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filePath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
    23                Excel.Worksheet xlWorksheet;
    24                // 循环所有DataTable
    25                forint i=0; i<ds.Tables.Count; i++ )
    26                {
    27                    // 添加入一个新的Sheet页。
    28                    xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing);
    29                    // 以TableName作为新加的Sheet页名。
    30                    xlWorksheet.Name = ds.Tables[i].TableName;
    31                    // 取出这个DataTable中的所有值,暂存于stringBuffer中。
    32                    string stringBuffer = "";
    33                    forint j=0; j<ds.Tables[i].Rows.Count; j++ )
    34                    {
    35                        forint k=0; k<ds.Tables[i].Columns.Count; k++ )
    36                        {
    37
    38                            stringBuffer += ds.Tables[i].Rows[j][k].ToString();
    39                            if( k < ds.Tables[i].Columns.Count - 1 )
    40                                stringBuffer += "\t";
    41                        }

    42                        stringBuffer += "\n";
    43                    }

    44                    // 利用系统剪切板
    45                    System.Windows.Forms.Clipboard.SetDataObject("");
    46                    // 将stringBuffer放入剪切板。
    47                    System.Windows.Forms.Clipboard.SetDataObject(stringBuffer);
    48                    // 选中这个sheet页中的第一个单元格
    49                    ((Excel.Range)xlWorksheet.Cells[1,1]).Select();
    50                    // 粘贴!
    51                    xlWorksheet.Paste(oMissing,oMissing);
    52                    // 清空系统剪切板。
    53                    System.Windows.Forms.Clipboard.SetDataObject("");
    54                }

    55                // 保存并关闭这个工作簿。
    56                xlWorkbook.Close( Excel.XlSaveAction.xlSaveChanges, oMissing, oMissing );
    57                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);
    58                xlWorkbook = null;
    59            }

    60            catch(Exception ex)
    61            {
    62                MessageBox.Show(ex.Message);
    63            }

    64            finally
    65            {
    66                // 释放
    67                xlApp.Quit();
    68                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
    69                xlApp = null;
    70                GC.Collect();
    71            }

    72        }

    73    }

    74}

    75
  • 相关阅读:
    【移动开发】Android应用程序中实用的代码框架(二)
    使用ListView应该注意的地方
    使用ListView应该注意的地方
    web技术文章
    [LeetCode] 565. Array Nesting
    [LeetCode]495. Teemo Attacking
    south 命令学习
    关于access_token过期的解决办法
    决策树总结《一》.md
    crontab命令
  • 原文地址:https://www.cnblogs.com/adam/p/669177.html
Copyright © 2011-2022 走看看