使用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 ...
随机推荐
- 天气预报数据API
http://www.weather.com.cn/data/cityinfo/101010100.html//过期: http://api.36wu.com/Weather/GetMoreWeath ...
- Windows Phone 8.1商店启动协议
最近开发wp8.1已经两个月了,感觉坑不少,原来8时代的商店api多明了,微软不给封装就算了,至少你要在msdn上明显的地方标注下啊...................顺便在吐槽下bing,找了一个 ...
- Controller将Model数据传给View层,View层应该如何处理?
首先,我们在Model层中添加一个Person类. namespace MVCTest.Models{ public class Person { public string ...
- 河南省第八届ACM程序设计大赛
A:挑战密室 #include <iostream> #include <cstdio> #include <cstring> #include <algor ...
- Install Solr+tomcat
1. chose the release you want: http://archive.apache.org/dist/lucene/solr/. 1) copy [solr_home]/dist ...
- hdu - 3959 Board Game Dice(数学)
这道题比赛中没做出来,赛后搞了好久才出来的,严重暴露的我薄弱的数学功底, 这道题要推公式的,,,有类似于1*a+2*a^2+3*a^3+...+n*a^n的数列求和. 最后画了一张纸才把最后的结果推出 ...
- Keil的标题“礦ision3" 的改变(转)
MDK 的标题显示成 “礦ision3",前面的这个不是u而是一个希腊字母“缪”,在中文显示中出现问题,半个汉字. 可以使用如下方法取消. 一: 光标问题 Keil uv3 中会出现光标定位 ...
- 【概念笔记】 EL表达式
一.EL简介 1.语法结构 ${expression} 2.[]与.运算符 EL 提供.和[]两种运算符来存取数据. 当要存取的属性名称中包含一些特殊字符,如.或?等并非字母或数字的符号,就一定要 ...
- JqueryEasyUI浅谈本地化应用
JqueryEasyUI浅谈本地化应用 Jquery是对javascript一种封装,使我们开发人员使用起来更加方便,同时也解决了不同浏览器中javascript的兼容性.JqueryEasyUi是基 ...
- 轻松实现ajax登录时让浏览器保存密码
将登录页面由form提交改为ajax提交,发现一个副作用——登录时浏览器不会提示是否保存密码,这样每次登录都要输入用户名/密码. html代码如下: <script> $(function ...