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. linux freetds操作mssql

    1.安装freetds wget http://mirrors.ibiblio.org/freetds/stable/freetds-stable.tgz tar xvzf freetds-stabl ...

  2. Titanium系列--安装Titanium Studio 中的Android SDK,JDK以及环境变量的配置(二)

    Ubuntu安装配置JDK 1.先去 Oracle下载Linux下的JDK压缩包,我下载的是jdk-8u25-linux-x64.tar.gz文件,下好后直接解压 Step1:# 将解压好的jdk1. ...

  3. Android的构造器

    当Java代码创建一个View实例,或根据XML布局文件加载并构建界面时将需要调用该构造器1.onFinishInflate():这是一个回调方法,当应用从XML布局文件加载该组件并利用它来构建界面之 ...

  4. MyEclipse 8.5 优化实例

    在用[MyEclipse] 写代码很容易卡死机,尤其是在对JSP文件的<%%>之间写代码的时候,只要一弹出智能提示就立刻卡死,程序失去响应,我以为是MyEclipse版本的问题,结果换了6 ...

  5. .net 架构师/经理招聘,长期有效

    岗位职责: 1.参与项目的需求分析和业务评审 2.负责项目的技术架构: 3.负责指导开发小组完成开发以及单元测试: 4.负责解决开发人员的技术问题,并对开发人员的代码进行Review. 任职要求: 1 ...

  6. 杂记- 3W互联网的圈子,大数据敏捷BI与微软BI的前端痛点

    开篇介绍 上周末参加了一次永洪科技在中关村 3W 咖啡举行的一次线下沙龙活动 - 关于它们的产品大数据敏捷 BI 工具的介绍.由此活动,我想到了三个话题 - 3W 互联网的圈子,永洪科技的大数据敏捷 ...

  7. Unity3D——键盘控制移动

    public float speed = 6f; Vector3 movement; void FixedUpdate () { float h = Input.GetAxisRaw("Ho ...

  8. 深入探索AngularJS(持续更新)

    数据双向绑定并不是Angular最出彩的地方.大部分对AngularJs的介绍都偏重于使用,使用的学习只是学了AngularJs的API,而那只能AngularJs的很小一部分.随着使用越来越深,系统 ...

  9. thrift之TTransport层的缓存传输类TBufferedTransport和缓冲基类TBufferBase

    本节主要介绍缓冲相关的传输类,缓存的作用就是为了提高读写的效率.Thrift在实现缓存传输的时候首先建立一个缓存的基类,然后需要实现缓存功能的类都可以直接从这个基类继承.下面就详细分析这个基类以及一个 ...

  10. 深入理解JavaScript 事件

    本文总结自<JavaScript高级程序设计>以及自己平时的经验,针对较新浏览器以及 DOM3 级事件标准(2016年8月),对少部分内容作了更正,增加了各种例子及解析. 如无特殊说明,本 ...