使用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 ...
随机推荐
- JS-定时器换背景
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- 【转】让Chrome化身成为摸鱼神器,利用Chorme运行布卡漫画以及其他安卓APK应用教程
下周就是十一了,无论是学生党还是工作党,大家的大概都会有点心不在焉,为了让大家更好的心不在焉,更好的在十一前最后一周愉快的摸鱼,今天就写一个如何让Chrome(google浏览器)运行安卓APK应用的 ...
- 一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球队,现在四个球队进行比赛,用一条sql 语句显示所有可能的比赛组合.
select *from timp a, timp b where a.name > b.name 结果:
- 第37讲:List的foldLeft、foldRight、sort操作代码实战
其实flodLeft和foldRight就是折叠操作,我让们看下下列的函数 折叠操作 def sum(xs:List[Int]):Int = ( 0 /: xs)(_ +_) def p ...
- 总结一下一般游戏中3D模型各种勾边方法遇到的工程性问题
以前做过简单的rim light勾边,几何勾边,这次又做了后处理的勾边,工程化的时候,都遇到很多问题,简单总结一下. 首先是火炬之光勾边效果,类似轮廓光的实现,简单的卡通渲染也是通过类似的算法加采样色 ...
- MySQL5.6 报错1067
http://www.cnblogs.com/lixiaolun/p/5303687.html
- Android资源命名规范
Android资源命名规范 命名模板为:缩写_主界面_功能部分(一) 缩写:ic ----------------------iconbg---------------------background ...
- [ACM_模拟][ACM_数学] LA 2995 Image Is Everything [由6个视图计算立方体最大体积]
Description Your new company is building a robot that can hold small lightweight objects. The robo ...
- C# 两行代码实现 延迟加载的单例模式(线程安全)
关键代码第4,5行. 很简单的原理不解释:readonly + Lazy(.Net 4.0 + 的新特性) public class LazySingleton { //Lazy singleton ...
- android-sdks/build-tools/17.0.0/aapt: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
fedora 23:dnf install zlib.i686 libstdc++.i686