1.单击butten在指定目录下创建excel表。(若路径不存在自动创建)
2.如何设置表头。
3.如何向指定坐标写入数据。
4.如何关闭表

解决方案 »

  1.   

    http://blog.csdn.net/simonllf/archive/2006/12/13/1441672.aspx看看高手写的吧。。我只是转贴!~
      

  2.   

    贴个简单的,用到Excel.dll#region (使用模版)将DataTable的数据导出显示为报表
            /// <summary>
            /// 将DataTable的数据导出显示为报表
            /// </summary>
            /// <param name="dt">要导出的数据</param>
            /// <param name="FilePath">保存文件的路径</param>
            /// <param name="SheetsNumber">导出报表的Sheet数</param>
            /// <param name="RowIndex">导出报表的起始行</param>
            /// <param name="ColIndex">导出报表的起始列</param>
            /// <param name="TemplateFile">模版文件</param>
            /// <returns></returns>
            public string OutputExcel(System.Data.DataTable dt, string FilePath, int SheetsNumber,int RowIndex, int ColIndex, string TemplateFile)
            {
                Excel.Application excel;
                Excel._Workbook xBk;
                Excel._Worksheet xSt = null;            excel = new Excel.ApplicationClass();
                xBk = excel.Workbooks._Open(@TemplateFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                
                for (int i = 0; i < SheetsNumber; i++)
                {
                    int index = i + 1;
                    int rowIndex = RowIndex;
                    xSt = (Excel.Worksheet)xBk.Worksheets.get_Item(index);                //取得表格中的数据
                    foreach (DataRow row in dt.Rows)
                    {
                        int colIndex = ColIndex;
                        rowIndex++;
                        foreach (DataColumn col in dt.Columns)
                        {
                            colIndex++;
                            if (col.DataType == System.Type.GetType("System.DateTime"))
                            {
                                xSt.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
                                //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
                            }
                            else
                                if (col.DataType == System.Type.GetType("System.String"))
                                {                                
                                    xSt.Cells[rowIndex, colIndex] = row[col.ColumnName];
                                    //xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
                                }
                                else
                                {
                                    xSt.Cells[rowIndex, colIndex] = row[col.ColumnName];
                                }
                        }
                    }
                                
                }            string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
                excel.ActiveWorkbook.SaveAs(FilePath + filename, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
                            #region  结束Excel进程
                                        
                xBk.Close(null, null, null);
                excel.Workbooks.Close();
                excel.Quit();            //注意:这里用到的所有Excel对象都要执行这个操作,否则结束不了Excel进程            
                if (xSt != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
                    xSt = null;
                }
                if (xBk != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
                    xBk = null;
                }
                if (excel != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                    excel = null;
                }
                GC.Collect();//垃圾回收
                #endregion            
            }