转载:C# Office 开发
原文地址:http://blog.sina.com.cn/s/blog_604fb7ae0100x2s7.html
中小企业办公自动化系统都需要有与微软办公软件连接的功能,如把数据导入到电子表格、Word等功能。C#.NET在Office方面提供了强大的功能,只要导入 Microsoft.Office.Interop.Excel 命名空间并调用此命名空间下的类,就可以在程序调用Excel、Word。
(一)Excel开发
首先导入 Microsoft.Office.Interop.Excel 命名空间
需要的类
_Application excel = new ApplicationClass(); //实例化对象
int rowIndex = 6;
int colIndex = 0;
_Workbook xBk;
_Worksheet xSt;
xBk = excel.Workbooks.Add(true);
xSt = (Microsoft.Office.Interop.Excel._Worksheet)xBk.ActiveSheet;
//取得列标题
for (int i = 0; i < dgvYingShouAmount.Columns.Count; i++){
colIndex++;
excel.Cells[rowIndex, colIndex] = Convert.ToString(dgvYingShouAmount.Columns[i].HeaderText);
}
//取得表格中的数据
for (int i = 0; i < dgvYingShouAmount.Rows.Count; i++){
rowIndex++;
colIndex = 0;
for (int j = 0; j < dgvYingShouAmount.Columns.Count; j++){
colIndex++;
excel.Cells[rowIndex, colIndex] =Convert.ToString(dgvYingShouAmount.Rows[i].Cells[j].Value);
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
}
}
excel.Cells[1, 1] = "***有限公司";
excel.Cells[2, 1] = "地址";
excel.Cells[3, 1] = "电话 传真";
excel.Cells[4, 1] = "客户对账单";
excel.Cells[5, 1] = "操作日期:" + dtpStartDate.Value.ToString("yyyy-MM-dd") + "/" + dtpEndDate.Value.ToString("yyyy-MM-dd");
//
//设置整个报表的标题格式
//
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Bold = true;
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]).Font.Size = 22;
xSt.get_Range(excel.Cells[3, 1], excel.Cells[3, 1]).Font.Bold = true;
//设置报表表格为最适应宽度
//
xSt.get_Range(excel.Cells[6, 2], excel.Cells[rowIndex, colIndex]).Select();
xSt.get_Range(excel.Cells[6, 2], excel.Cells[rowIndex, colIndex]).Columns.AutoFit();
//设置整个报表的标题为跨列居中
//
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).Select();
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
xSt.get_Range(excel.Cells[2, 1], excel.Cells[2, colIndex]).Select();
xSt.get_Range(excel.Cells[2, 1], excel.Cells[2, colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
xSt.get_Range(excel.Cells[3, 1], excel.Cells[3, colIndex]).Select();
xSt.get_Range(excel.Cells[3, 1], excel.Cells[3, colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
xSt.get_Range(excel.Cells[4, 1], excel.Cells[4, colIndex]).Select();
xSt.get_Range(excel.Cells[4, 1], excel.Cells[4, colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
xSt.get_Range(excel.Cells[5, 1], excel.Cells[5, colIndex]).Select();
xSt.get_Range(excel.Cells[5, 1], excel.Cells[5, colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
xSt.get_Range(excel.Cells[6, 1], excel.Cells[rowIndex, colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[6, 1], excel.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置左边线加粗
xSt.get_Range(excel.Cells[6, 1], excel.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置上边线加粗
xSt.get_Range(excel.Cells[6, 1], excel.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置右边线加粗
xSt.get_Range(excel.Cells[6, 1], excel.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThick;//设置下边线加粗
excel.Visible = true;
string file = "保存的路径";
xBk.SaveCopyAs(file);
以下为我仿写的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; using MSExcel = Microsoft.Office.Interop.Excel; namespace testoffice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
MSExcel._Application excel = new MSExcel.ApplicationClass(); int rowIndex = , colIndex = , myCCount = , myRCount = ; MSExcel._Workbook xBk = null;
MSExcel._Worksheet xSt = null; xBk = excel.Workbooks.Add(true);
xSt = (MSExcel._Worksheet)xBk.ActiveSheet; //
for (int i = ; i < myCCount; i++)
{
colIndex++;
xSt.Cells[rowIndex, colIndex] = "标题" + colIndex.ToString();
} //
for (int i = ; i < myRCount; i++)
{
rowIndex++;
colIndex = ;
for (int j = ; j < myCCount; j++)
{
colIndex++;
xSt.Cells[rowIndex, colIndex] = "内容" + rowIndex.ToString() + ":" + colIndex.ToString();
xSt.get_Range(xSt.Cells[rowIndex, colIndex], xSt.Cells[rowIndex, colIndex]).HorizontalAlignment = MSExcel.XlHAlign.xlHAlignCenter;
}
} xSt.Cells[, ] = "宇宙无限公司";
xSt.Cells[, ] = "地址";
xSt.Cells[, ] = "电话 传真";
xSt.Cells[, ] = "客户对账单";
xSt.Cells[, ] = "操作日期:" + DateTime.Now.ToLongTimeString(); //
xSt.get_Range(xSt.Cells[, ], xSt.Cells[, ]).Font.Bold = true;
xSt.get_Range(xSt.Cells[, ], xSt.Cells[, ]).Font.Size = ;
xSt.get_Range(xSt.Cells[, ], xSt.Cells[, ]).Font.Bold = true; xSt.get_Range(xSt.Cells[, ], xSt.Cells[, ]).Font.Bold = true; //
for (int i = ; i < ; i++)
{
xSt.get_Range(xSt.Cells[i, ], xSt.Cells[i, colIndex]).Select();
xSt.get_Range(xSt.Cells[i, ], xSt.Cells[i, colIndex]).HorizontalAlignment = MSExcel.XlHAlign.xlHAlignCenterAcrossSelection;
} //
xSt.get_Range(xSt.Cells[, ], xSt.Cells[rowIndex, colIndex]).Borders.LineStyle = ;
xSt.get_Range(xSt.Cells[, ], xSt.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = MSExcel.XlBorderWeight.xlThick;
xSt.get_Range(xSt.Cells[, ], xSt.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = MSExcel.XlBorderWeight.xlThick;
xSt.get_Range(xSt.Cells[, ], xSt.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = MSExcel.XlBorderWeight.xlThick;
xSt.get_Range(xSt.Cells[, ], xSt.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = MSExcel.XlBorderWeight.xlThick; excel.Visible = true;
string file = "e:/testexcel.xlsx";
xBk.SaveCopyAs(file);
}
}
}
转载:C# Office 开发的更多相关文章
- Office 开发版本号与版本对应关系
Office 开发版本号与版本对应关系: office97 : 8.0 office2000 : 9.0 officeXP(2002) : 10.0 office2003 : 11.0 office2 ...
- [转载]Android相关开发网站
my: Android 开发官方文档国内镜像-踏得网: http://wear.techbrood.com/index.html 转载自: http://my.oschina.net/luforn/b ...
- 转载:做Java开发这一年 (火龙果软件)
转载:http://www.uml.org.cn/success/201410205.asp 从去年到现在,从.NET转向Java开发(只是因为项目原因,绝对与平台好坏没有关系)差不多有一年的时间了. ...
- Office开发必备知识----为什么要释放非托管Com资源
https://www.cnblogs.com/Charltsing/p/RealeaseComObject.html QQ:564955427 目前,国内Office插件开发的风头正盛,很多VBAe ...
- 【转载】Office软件自定义功能区不完全显示修复方法
转载地址:http://www.doudouxitong.net/guzhang/xitongjiqiao/2015/0603/8822.html 豆豆系统 Office是比较常用的办公软件,我们也会 ...
- 转载-iOS SDK开发
最近帮兄弟公司的做支付业务sdk,积累了 sdk 封装的经验!下面我会从零开始把我的 sdk 封装和调试经历分享给大家,希望能给看到这篇文章的人有所帮助! 本文我会从以下几个方面来讲述: Framew ...
- [转载].NET Web开发技术(补充)
大家在工作应该养成善于总结的习惯,总结你所学习.使用的技术,总结你所工作事项的比较好的地方,善于总结不断的沉淀优化自己.适时停下来总结下过去走过的路,才能让我们的未来走的更坚定.文章转自JamesLi ...
- 【转载】Web开发技术发展历史-版本1
原文在这里. Web开发技术发展历史 Web的诞生 提到Web,不得不提一个词就是“互联网”.Web是World Wide Web的简称,中文译为万维网.“万维网”和我们经常说的“互联网”是两个联系极 ...
- (转载) Android开发mac /dev/kvm is not found
Android开发mac /dev/kvm is not found 标签: KVMAndroid开发KVM is not found芒果Android芒果iOS 2016-10-29 16:31 2 ...
随机推荐
- OC基础2:一些基本概念
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.字符常量是存放在单引号中的单个字符,字 ...
- CXF发布webservice入门
1.设置CXF的bin目录进环境变量 2.CXF导入相关的jar包. 3.建立接口 @WebService public interface HelloWorld { public void say( ...
- Memcached安装卸载
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached ...
- 【转】zookeeper 的监控工具
公司很多产品会使用zookeeper,比如Meta消息中间件,在测试的过程中,我们经常需要查询zookeeper里面的信息来精确定位问题.目前项目中有开发团队自己写的浏览器node-z ...
- Malloc碎碎念
(以前为给同学分享写的点东西,很基础.)现在的比赛中堆溢出非常常见,对于glibc下malloc的理解也要深入一些. malloc_chunk的对齐属性 在glibc中,malloc_chunk以 2 ...
- CXF interceptor拦截顺序
CXF Interceptor中Phase的先后顺序 org.apache.cxf.phase.PhaseManagerImpl中 final void createInPhases() { int ...
- JavaScript检测之basevalidate.js
上篇文章「JavaScript检测原始值.引用值.属性」中涉及了大量有用的代码范例,为了让大家更方便的使用这些代码,博主特意把这些代码重新整理并托管到 GitHub,项目地址是:https://git ...
- JS中的逻辑哲学
1.幻灯片播放. 有重用功能的代码要封入一个函数内,尽量减少调用出口(一般传入的参数为索引值),以便调用: 计数器放在最终调用的函数那里,index++: 明确那部分函数执行什么功能,将代码块只放在相 ...
- UVA1600 Patrol Robot
题意: 求机器人走最短路线,而且可以穿越障碍.N代表有N行,M代表最多能一次跨过多少个障碍. 分析: bfs()搜索,把访问状态数组改成了3维的,加了个维是当前能跨过的障碍数. 代码: #includ ...
- sqlserver高版本到低版本迁移
从SqlServer2014 备份bak文件,在SqlServer2012中还原时遇到版本不兼容的问题. 经过leader 指导,可尝试以下办法处理数据迁移.共分三个步骤: 一.对需要迁移数据库生成s ...