object filePath = @"C:\" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.ToLongTimeString().Replace(":", "") + ".xlsx";   //文件保存路径

        public void GeneExcel(DataSet ds)
{
Excel.ApplicationClass xlApp = new Excel.ApplicationClass(); Workbooks workbooks = xlApp.Workbooks;
Workbook workBook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet workSheet = (Worksheet)workBook.Worksheets[];//取得sheet1 //打开一个WorkBook
//Workbooks workbooks = xlApp.Workbooks;
//Workbook workBook = xlApp.Workbooks.Open(filePath.ToString(),
// Type.Missing, Type.Missing, Type.Missing, Type.Missing,
// Type.Missing, Type.Missing, Type.Missing, Type.Missing,
// Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); ////得到WorkSheet对象
//Worksheet workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(1); try
{
workSheet.Name = "表单一"; workSheet.Cells[, ] = "单元名称";
workSheet.Cells[, ] = "指标及其描述、特征值和权重";
workSheet.Cells[, ] = "属性";
workSheet.Cells[, ] = "D";
workSheet.Cells[, ] = "R";
workSheet.Cells[, ] = "A";
workSheet.Cells[, ] = "S";
workSheet.Cells[, ] = "T";
workSheet.Cells[, ] = "I";
workSheet.Cells[, ] = "C";
workSheet.Cells[, ] = "结果";
workSheet.Cells[, ] = "等级";
workSheet.Cells[, ] = "描述";
workSheet.Cells[, ] = "特征值";
workSheet.Cells[, ] = "权重";
workSheet.Cells[, ] = "描述";
workSheet.Cells[, ] = "特征值";
workSheet.Cells[, ] = "权重";
workSheet.Cells[, ] = "描述";
workSheet.Cells[, ] = "特征值";
workSheet.Cells[, ] = "权重";
workSheet.Cells[, ] = "描述";
workSheet.Cells[, ] = "特征值";
workSheet.Cells[, ] = "权重";
workSheet.Cells[, ] = "描述";
workSheet.Cells[, ] = "特征值";
workSheet.Cells[, ] = "权重";
workSheet.Cells[, ] = "描述";
workSheet.Cells[, ] = "特征值";
workSheet.Cells[, ] = "权重";
workSheet.Cells[, ] = "描述";
workSheet.Cells[, ] = "特征值";
workSheet.Cells[, ] = "权重"; workSheet.get_Range("A1", "A3").MergeCells = true; //合并单元格
Range rg1 = workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]);
//range.ClearContents(); //先把Range内容清除,合并才不会出错
rg1.MergeCells = true;
rg1.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
rg1.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter; workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]).MergeCells = true;
workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]).MergeCells = true;
workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]).MergeCells = true;
workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]).MergeCells = true;
workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]).MergeCells = true;
workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]).MergeCells = true;
workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]).MergeCells = true;
workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]).MergeCells = true;
workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]).MergeCells = true;
workSheet.get_Range(workSheet.Cells[, ], workSheet.Cells[, ]).MergeCells = true; /*往Excel格式表格中写入数据*/
int waterdataRows = ds.Tables[].Rows.Count;
int waterdataCols = ds.Tables[].Columns.Count;
for (int i = ; i <= waterdataRows; i++)
{
workSheet.Cells[i + , ] = ds.Tables[].Rows[i - ].ItemArray.GetValue();
for (int j = ; j <= ; j++)
{
workSheet.Cells[i + , * j] = ds.Tables[].Rows[i - ].ItemArray.GetValue(j);
}
workSheet.Cells[i + , ] = ds.Tables[].Rows[i - ].ItemArray.GetValue();
workSheet.Cells[i + , ] = ds.Tables[].Rows[i - ].ItemArray.GetValue();
} //插入空行
//Range rgInsert = (Excel.Range)workSheet.Rows[6, Type.Missing];
//rgInsert.Insert(Excel.XlDirection.xlDown, Type.Missing);
//rgInsert.Copy(); //复制
//rgInsert.Delete(); //删除
//range1.Copy(rgStyle); //自动填充(如星期一到星期五)
//Excel.Range rng = workSheet.get_Range("B4", Type.Missing);
//rng.Value2 = "星期一 ";
//rng.AutoFill(workSheet.get_Range("B4", "B9"),
// Excel.XlAutoFillType.xlFillWeekdays); Excel.Range rng = workSheet.get_Range("C4", Type.Missing);
rng.Value2 = "一月";
rng.AutoFill(workSheet.get_Range("C4", "C9"),
Excel.XlAutoFillType.xlFillMonths); //rng.Value2 = "1";
//rng.AutoFill(workSheet.get_Range("D4", "D9"),
// Excel.XlAutoFillType.xlFillSeries); //Excel.Range rangePic = workSheet.get_Range("A10", "A11");
//rangePic.Select();
//workSheet.Shapes.AddPicture(@"C:\6.jpg", Microsoft.Office.Core.MsoTriState.msoFalse,
//Microsoft.Office.Core.MsoTriState.msoTrue, Convert.ToSingle(rangePic.Left), Convert.ToSingle(rangePic.Top), 10, 10); #region 应用样式
Excel.Range rgStyle = workSheet.get_Range("O10", Type.Missing);
rgStyle.Formula = "=SUM(O4:O9)";
rgStyle.Calculate();
//rgStyle.Interior.ColorIndex = ColorIndex.红色;//15背景色
//rgStyle.Font.Color = ColorIndex.白色; //无效
//rgStyle.Font.Bold = true;
//rgStyle.Font.Size = 9; Excel.Style style;
//style = workBook.Styles["NewStyle"];
style = workBook.Styles.Add("NewStyle", Type.Missing);
style.Font.Name = "Verdana";
style.Font.Size = ;
style.Font.Color = ;//前景色
style.Interior.Color = ( << ) | ( << ) | ; //背景色
//rgStyle.Interior.ColorIndex = ColorIndex.红色;//15背景色
style.Interior.Pattern = Excel.XlPattern.xlPatternSolid;
rgStyle.Value2 = "'Style Test";
rgStyle.Style = "NewStyle";
rgStyle.Columns.AutoFit();
#endregion #region 行高、列宽
//Range rgHeight = workSheet.get_Range("C9", Type.Missing);
//rgHeight.Value = "sdfsdfsdf";
//rgHeight.RowHeight = 100;
//range.EntireColumn.AutoFit(); Range rgWidth = workSheet.get_Range("C9", Type.Missing);
rgWidth.Value = "sdfsfscvfffffffffffdf";
//rgWidth.ColumnWidth = 20;
rgWidth.EntireColumn.AutoFit(); //自动根据内容设定宽度
#endregion //sheet.Visible = Excel.XlSheetVisibility.xlSheetHidden; //隐藏工作表 #region 保存Excel
workBook.Saved = true;
//workBook.Save();
//workBook.SaveCopyAs(filePath);
workBook.SaveAs(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//workBook.SaveAs(filePath, Excel.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//Excel.XlFileFormat.xlXMLSpreadsheet 生成多种格式文件 SaveAsFileFormat枚举 #endregion #region 释放Excel资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(rg1); //释放Range
System.Runtime.InteropServices.Marshal.ReleaseComObject(rgStyle);
System.Runtime.InteropServices.Marshal.ReleaseComObject(rgWidth);
if (workSheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
}
if (workBook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
workBook = null;
}
if (workbooks != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
workbooks = null;
}
if (xlApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
} workBook.Close(false, null, null);
xlApp.Workbooks.Close();
xlApp.Quit(); //自动打开Excel,抛异常 GC.WaitForPendingFinalizers();
GC.Collect();
#endregion
}
catch (Exception ex)
{
}
finally
{ } System.Diagnostics.Process.Start(filePath.ToString());
}

转 http://www.cnblogs.com/gossip/archive/2011/10/22/2221465.html

使用Microsoft.Office.Interop.Excel.Application xlApp 生成Excel的更多相关文章

  1. NPOI写Excel,Microsoft.Office.Interop.excel.dll 转换Excel为PDF

    首先要引用NPOI动态库和Microsoft.Office.Interop.excel.dll (Microsoft.Office.Interop.excel.dll 下载链接 ,下载以后解压文件,把 ...

  2. 用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  3. C#用Microsoft.Office.Interop.Word进行Word转PDF的问题

    之前用Aspose.Word进行Word转PDF发现'\'这个字符会被转换成'¥'这样的错误,没办法只能换个方法了.下面是Microsoft.Office.Interop.Word转PDF的方法: p ...

  4. 使用Microsoft.Office.Interop.Excel.dll 文件来生成excel 文件

    日常工作中经常需要将后台的数据导出成excel  格式,这里通过调用微软提供的类库来生成excel 文件. 具体是引用 了Microsoft.Office.Interop.Excel.dll 类库文件 ...

  5. Visual Studio 进行Excel相关开发,Microsoft.Office.Interop.Excel.dll库

    1. Interop.Excel.dll 的查找 本文中将 Microsoft.Office.Interop.Excel.dll库简称为Interop.Excel.dll库 其实在使用Visual S ...

  6. 引用Microsoft.Office.Interop.Excel出现的问题

    引用Microsoft.Office.Interop.Excel出现的问题   转自:http://www.hccar.com/Content,2008,6,11,75.aspx,作者:方继祥 操作背 ...

  7. C# Microsoft.Office.Interop.Owc11 导出excel文件

    C# Microsoft.Office.Interop.Owc11 导出excel文件 1.新建项SupremeWindowsForms窗体应用项目(项目平台设置称X86) 注意:因为大多数第三方写的 ...

  8. Microsoft.Office.Interop.Excel Find 操作

    public void SearchLoactions(string filepath, int start, int end ,string expectvalue) { if (end >= ...

  9. Microsoft.Office.Interop.Excel的用法以及利用Microsoft.Office.Interop.Excel将web页面转成PDF

    1.常见用法           using Microsoft.Office.Interop.Excel; 1)新建一个Excel ApplicationClass ExcelApp = New A ...

随机推荐

  1. 【kd-tree】bzoj2850 巧克力王国

    分四种情况讨论:a,b>=0 a,b<0 a>=0,b<0 a<0,b>=0 然后每次检验是否进入一个矩形框 或者 是否直接利用这个矩形框的答案 仅仅利用两个对角的 ...

  2. git --如何撤销已放入缓存区(Index区)的修改

    修改或新增的文件通过 git add --all 命令全部加入缓存区(index区)之后,使用 git status 查看状态(git status -s 简单模式查看状态,第一列本地库和缓存区的差异 ...

  3. h5图片上传预览与拖拽上传

    图片上传: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  4. zabbix_agentd安装脚本共享

    #!/bin/bash wget -P /expect_zabbix/ http://10.107.2.44/zabbix_agent/check.sh . /expect_zabbix/check. ...

  5. 解决 yum安装时报错 Error: Protected multilib versions: 报错

    系统中缺少一个lib库 libz.so.1文件,使用yum安装会自动找到相关的rpm包,如下命令 # yum -y install libz.so.1 Resolving Dependencies-- ...

  6. Chrome RenderText分析(2)

      接Chrome RenderText分析(1) 继续分析以下步骤   一.TextRun结构 struct TextRun { TextRun(); ~TextRun(); ui::Range r ...

  7. onFocus="this.blur()"的解释

    onFocus="this.blur()" onFocus即获取焦点的意思,而blur却是失去焦点的意思,因此onFocus="this.blur()"的通俗理 ...

  8. Windows Phone编程中Dispatcher.BeginInvoke的使用

    在学习Windows Phone 程序开发时经常会使用到Dispatcher.BeginInvoke()的编程方法,可能许多初学者并不熟悉Dispatcher.BeginInvoke的使用方法以及为什 ...

  9. HTTP权威指南阅读笔记四:连接管理

    HTTP通信是由TCP/IP承载的,HTTP紧挨着TCP,位于其上层,所以HTTP事务的性能很大程度上取决于底层TCP通道的性能. HTTP事务的时延 如图: HTTP事务的时延有以下几种主要原因. ...

  10. 如何在C#中生成与PHP一样的MD5 Hash Code

    最近在对一个现有的系统进行C#改造,该系统以前是用PHP做的,后台的管理员登陆用的是MD5加密算法.在PHP中,要对一个字符串进行MD5加密非常简单,一行代码即可: md5("Somethi ...