---
title: "导出Excel"
author: "gauss2008"
date: "2006-12-25T10:11:00Z"
tags: []
slug: "excel"
draft: false
lastmod: "2006-12-25T10:11:00Z"
id: 74706
url: "https://gauss2008.iteye.com/blog/74706"
---

如何将数据导入到Excel，方法大体分为两种。一是以数据流的形式写入到文件，另外一种就是调用Microsoft的Excel.dll。 今天主要介绍后者， 如何根据传入数据和显示格式自动的生成Excel。首先看源代码

 

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System.Collections.Generic;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System.Text;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System.Data;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System.Collections;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System.IO;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System.Diagnostics;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using Excel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System.Runtime.InteropServices;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)namespace Gauss.Common  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public class GaussExcel  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                // Fields  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private DateTime afterTime;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private DateTime beforeTime;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private int titleColorindex;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private int topPargin;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private int leftPargin;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private bool showBlank;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public bool ShowBlank  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            get ...{ return showBlank; }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            set ...{ showBlank = value; }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public int LeftPargin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            get ...{ return leftPargin; }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            set ...{ leftPargin = value; }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public int TopPargin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            get ...{ return topPargin; }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            set ...{ topPargin = value; }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public GaussExcel()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            this.titleColorindex = 15;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            this.leftPargin = 2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            this.topPargin = 2;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            this.showBlank = true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private void ClearFile(string FilePath)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            string\[] textArray1 = Directory.GetFiles(FilePath);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            if (textArray1.Length &gt; 10)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                for (int num1 = 0; num1 &lt; 10; num1++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        File.Delete(textArray1\[num1]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    catch  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public string ProduceExcel(System.Data.DataTable dt, string strTitle, string FilePath, Hashtable nameList)   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            string fileName=null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            ClearFile(FilePath);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            fileName = OutputExcel(dt, strTitle, FilePath, nameList);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            KillExcelProcess();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            return fileName;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public void KillExcelProcess()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            Process\[] processArray1 = Process.GetProcessesByName("Excel");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            foreach (Process process1 in processArray1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                DateTime time1 = process1.StartTime;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                if ((time1 &gt; this.beforeTime) && (time1 &lt; this.afterTime))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    process1.Kill();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public string OutputExcel(System.Data.DataTable dt, string strTitle, string FilePath, Hashtable nameList)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            this.beforeTime = DateTime.Now;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            //FiledName  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            int num1 = this.topPargin+1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            if (this.showBlank) num1 += 1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            int numTitle = num1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            int num2 = this.leftPargin-1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            Application application1 = new ApplicationClass();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            \_Workbook workbook1 = application1.Workbooks.Add(true);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            \_Worksheet worksheet1 = (\_Worksheet)workbook1.ActiveSheet;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            foreach (DataColumn column1 in dt.Columns)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                bool myFlag = false;                  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                string text5 = column1.ColumnName.Trim();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                if (nameList.ContainsKey(text5))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    num2++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    application1.Cells\[num1, num2] = nameList\[text5];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    worksheet1.get\_Range(application1.Cells\[num1, num2], application1.Cells\[num1, num2]).Font.Bold = true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    worksheet1.get\_Range(application1.Cells\[num1, num2], application1.Cells\[num1, num2]).HorizontalAlignment = XlVAlign.xlVAlignCenter;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    worksheet1.get\_Range(application1.Cells\[num1, num2], application1.Cells\[num1, num2]).Select();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    worksheet1.get\_Range(application1.Cells\[num1, num2], application1.Cells\[num1, num2]).Interior.ColorIndex = this.titleColorindex;    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)              /\*\*//*  IDictionaryEnumerator enumerator1 = nameList.GetEnumerator();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                while (enumerator1.MoveNext())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    if (enumerator1.Key.ToString().Trim() == text5)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        text5 = enumerator1.Value.ToString() ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        myFlag = true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                if (myFlag) {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    num2++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    application1.Cells\[num1, num2] = text5;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    worksheet1.get\_Range(application1.Cells\[num1, num2], application1.Cells\[num1, num2]).Font.Bold = true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    worksheet1.get\_Range(application1.Cells\[num1, num2], application1.Cells\[num1, num2]).HorizontalAlignment = XlVAlign.xlVAlignCenter;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    worksheet1.get\_Range(application1.Cells\[num1, num2], application1.Cells\[num1, num2]).Select();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    worksheet1.get\_Range(application1.Cells\[num1, num2], application1.Cells\[num1, num2]).Interior.ColorIndex = this.titleColorindex;    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }    \*/                           
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            //Fill In Data To Excel