使用Microsoft.Office.Interop.Excel.Application xlApp 生成Excel
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的更多相关文章
- NPOI写Excel,Microsoft.Office.Interop.excel.dll 转换Excel为PDF
首先要引用NPOI动态库和Microsoft.Office.Interop.excel.dll (Microsoft.Office.Interop.excel.dll 下载链接 ,下载以后解压文件,把 ...
- 用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- C#用Microsoft.Office.Interop.Word进行Word转PDF的问题
之前用Aspose.Word进行Word转PDF发现'\'这个字符会被转换成'¥'这样的错误,没办法只能换个方法了.下面是Microsoft.Office.Interop.Word转PDF的方法: p ...
- 使用Microsoft.Office.Interop.Excel.dll 文件来生成excel 文件
日常工作中经常需要将后台的数据导出成excel 格式,这里通过调用微软提供的类库来生成excel 文件. 具体是引用 了Microsoft.Office.Interop.Excel.dll 类库文件 ...
- Visual Studio 进行Excel相关开发,Microsoft.Office.Interop.Excel.dll库
1. Interop.Excel.dll 的查找 本文中将 Microsoft.Office.Interop.Excel.dll库简称为Interop.Excel.dll库 其实在使用Visual S ...
- 引用Microsoft.Office.Interop.Excel出现的问题
引用Microsoft.Office.Interop.Excel出现的问题 转自:http://www.hccar.com/Content,2008,6,11,75.aspx,作者:方继祥 操作背 ...
- C# Microsoft.Office.Interop.Owc11 导出excel文件
C# Microsoft.Office.Interop.Owc11 导出excel文件 1.新建项SupremeWindowsForms窗体应用项目(项目平台设置称X86) 注意:因为大多数第三方写的 ...
- Microsoft.Office.Interop.Excel Find 操作
public void SearchLoactions(string filepath, int start, int end ,string expectvalue) { if (end >= ...
- Microsoft.Office.Interop.Excel的用法以及利用Microsoft.Office.Interop.Excel将web页面转成PDF
1.常见用法 using Microsoft.Office.Interop.Excel; 1)新建一个Excel ApplicationClass ExcelApp = New A ...
随机推荐
- HtmlHelper和强类型转换
MVC HtmlHelper;1.Url():<%= Html.ActionLink("用户列表","方法","控制器") %> ...
- React Native填坑之旅--ListView篇
列表显示数据,基本什么应用都是必须.今天就来从浅到深的看看React Native的ListView怎么使用.笔者写作的时候RN版本是0.34. 最简单的 //@flow import React f ...
- Linux下paste命令
paste 用于将多个文件按照列队列进行合并. 该命令主要用来将多个文件的内容合并,与cut命令完成的功能刚好相反. 1.原文件: 1>a.txt [root@localhost home]# ...
- 对SQLSERVER进行性能监控
对SQLSERVER进行性能监控 在上一篇文章<SQLSERVER性能监控级别步骤>里说到性能监控的步骤中有一步涉及到建立性能基线,但是没有说到有哪些计数器 可以用来进行监控的,这篇文章结 ...
- 敏捷开发 与 Scrum
敏捷开发以用户的需求进化为核心,采用迭代.循序渐进的方法进行软件开发.在敏捷开发中,软件项目在构建初期被切分成多个子项目,各个子项目的成果都经过测试,具备可视.可集成和可运行使用的特征.换言之,就是把 ...
- solr与.net系列课程(二)solr的配置文件及其含义
solr与.net系列课程(二)solr的配置文件及其含义 本节内容还是不会涉及到.net与数据库的内容,但是不要着急,这都是学时solr必学要掌握的东西,solr可不是像其他的dll文件一样,只需 ...
- 在stackoverflow上使用markdown
stackoverflow流派的markdown. Code and Preformatted Text 缩进四个空格,就可以写代码片段了 def hello(): print "hello ...
- 从源代码分析Android-Universal-Image-Loader的缓存处理机制
讲到缓存,平时流水线上的码农一定觉得这是一个高大上的东西.看过网上各种讲缓存原理的文章,总感觉那些文章讲的就是玩具,能用吗?这次我将带你一起看过UIL这个国内外大牛都追捧的图片缓存类库的缓存处理机制. ...
- Android资源命名规范
Android资源命名规范 命名模板为:缩写_主界面_功能部分(一) 缩写:ic ----------------------iconbg---------------------background ...
- 如何试用Office 365 及 SharePoint Online(美版)
Office 365已经在国外运营一段时间了,本文主要帮助大家注册一个试用账户.废话少说按步骤来: 进入注册页面,链接地址 . 1. 对于不同的企业,提供了不同的套餐,这里我们试用这个中等企业的套餐, ...