nieyangjie

导出excel

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Data;
using Microsoft.Office.InfoPath;
namespace ClassLibrary1
{
    public class Class1
    {
   /// <summary>
        /// 动态生成XSL,并写入XML流
        /// </summary>
        /// <param name="writer">XML流 </param>
        /// <param name="headers">表头数组 </param>
        /// <param name="fields">字段数组 </param>
        /// <param name="exportFormat">导出文件的格式 </param>
        private static void CreateStylesheet(XmlTextWriter writer, string[] headers, string[] fields, ExportFormat exportFormat)
        {
            string ns = "http://www.w3.org/1999/XSL/Transform";
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("xsl", "stylesheet", ns);
            writer.WriteAttributeString("version", "1.0");
            writer.WriteStartElement("xsl:output");
            writer.WriteAttributeString("method", "text");
            writer.WriteAttributeString("version", "4.0");
            writer.WriteEndElement();

            // xsl-template
            writer.WriteStartElement("xsl:template");
            writer.WriteAttributeString("match", "/");

            // xsl:value-of for headers
            for (int i = 0; i < headers.Length; i++)
            {
                writer.WriteString("\"");
                writer.WriteStartElement("xsl:value-of");
                writer.WriteAttributeString("select", "'" + headers[i] + "'");
                writer.WriteEndElement(); // xsl:value-of
                writer.WriteString("\"");
                if (i != fields.Length - 1) writer.WriteString((exportFormat == ExportFormat.CSV) ? "," : " ");
            }
            // xsl:for-each
            writer.WriteStartElement("xsl:for-each");
            writer.WriteAttributeString("select", "Export/Values");
            writer.WriteString("\r\n");

            // xsl:value-of for data fields
            for (int i = 0; i < fields.Length; i++)
            {
                writer.WriteString("\"");
                writer.WriteStartElement("xsl:value-of");
                writer.WriteAttributeString("select", fields[i]);
                writer.WriteEndElement(); // xsl:value-of
                writer.WriteString("\"");
                if (i != fields.Length - 1) writer.WriteString((exportFormat == ExportFormat.CSV) ? "," : " ");
            }

            writer.WriteEndElement(); // xsl:for-each
            writer.WriteEndElement(); // xsl-template
            writer.WriteEndElement(); // xsl:stylesheet
        }     
        /// <summary>
        /// 导出数据源的数据
        /// </summary>
        /// <param name="dt">数据源 </param>
        /// <param name="columnIndexList">导出的列索引数组 </param>
        /// <param name="exportFormat">导出文件的格式 </param>
        /// <param name="fileName">输出文件名 </param>
        /// <param name="encoding">编码 </param>
        public static void Export(DataTable dt, int[] columnIndexList, ExportFormat exportFormat, string fileName, Encoding encoding)
        {
            DataSet dsExport = new DataSet("Export");
            DataTable dtExport = dt.Copy();

            dtExport.TableName = "Values";
            dsExport.Tables.Add(dtExport);

            string[] headers = new string[columnIndexList.Length];
            string[] fields = new string[columnIndexList.Length];

            for (int i = 0; i < columnIndexList.Length; i++)
            {
                headers[i] = dtExport.Columns[columnIndexList[i]].ColumnName;
                fields[i] = ReplaceSpecialChars(dtExport.Columns[columnIndexList[i]].ColumnName);
            }
            Export(dsExport, headers, fields, exportFormat, fileName, encoding);
        }
        /// <summary>
        /// 导出数据源的数据
        /// </summary>
        /// <param name="dt">数据源 </param>
        /// <param name="columnIndexList">导出的列索引数组 </param>
        /// <param name="headers">导出的列标题数组 </param>
        /// <param name="exportFormat">导出文件的格式 </param>
        /// <param name="fileName">输出文件名 </param>
        /// <param name="encoding">编码 </param>
        public static void Export(DataTable dt, int[] columnIndexList, string[] headers, ExportFormat exportFormat, string fileName, Encoding encoding)
        {
            DataSet dsExport = new DataSet("Export");
            DataTable dtExport = dt.Copy();
            dtExport.TableName = "Values";
            dsExport.Tables.Add(dtExport);
            string[] fields = new string[columnIndexList.Length];
            for (int i = 0; i < columnIndexList.Length; i++)
            {
                fields[i] = ReplaceSpecialChars(dtExport.Columns[columnIndexList[i]].ColumnName);
            }
            Export(dsExport, headers, fields, exportFormat, fileName, encoding);
        }       
    }
}

   









调用
case "ExportExcel":
                    webprint.FileSave.Export(dt,ExportFormat.XLS,"excel",Encoding.GetEncoding("GB2312"));                   
                    break;
                case "ExportCsv":
                    webprint.FileSave.Export(dt, ExportFormat.CSV, "excel", Encoding.GetEncoding("GB2312")); 
                    break;
                case "ExportWord":
                    webprint.FileSave.Export(dt, ExportFormat.DOC, "word", Encoding.GetEncoding("GB2312")); 
                    break;
                case "ExportText":
                    webprint.FileSave.Export(dt, ExportFormat.TXT, "text", Encoding.GetEncoding("GB2312")); 
                    break;

posted on 2009-08-27 21:51  nieyangjie  阅读(302)  评论(0编辑  收藏  举报

导航