原文地址: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);

以下为我仿写的代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. using MSExcel = Microsoft.Office.Interop.Excel;
  10.  
  11. namespace testoffice
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void button1_Click(object sender, EventArgs e)
  21. {
  22. MSExcel._Application excel = new MSExcel.ApplicationClass();
  23.  
  24. int rowIndex = , colIndex = , myCCount = , myRCount = ;
  25.  
  26. MSExcel._Workbook xBk = null;
  27. MSExcel._Worksheet xSt = null;
  28.  
  29. xBk = excel.Workbooks.Add(true);
  30. xSt = (MSExcel._Worksheet)xBk.ActiveSheet;
  31.  
  32. //
  33. for (int i = ; i < myCCount; i++)
  34. {
  35. colIndex++;
  36. xSt.Cells[rowIndex, colIndex] = "标题" + colIndex.ToString();
  37. }
  38.  
  39. //
  40. for (int i = ; i < myRCount; i++)
  41. {
  42. rowIndex++;
  43. colIndex = ;
  44. for (int j = ; j < myCCount; j++)
  45. {
  46. colIndex++;
  47. xSt.Cells[rowIndex, colIndex] = "内容" + rowIndex.ToString() + ":" + colIndex.ToString();
  48. xSt.get_Range(xSt.Cells[rowIndex, colIndex], xSt.Cells[rowIndex, colIndex]).HorizontalAlignment = MSExcel.XlHAlign.xlHAlignCenter;
  49. }
  50. }
  51.  
  52. xSt.Cells[, ] = "宇宙无限公司";
  53. xSt.Cells[, ] = "地址";
  54. xSt.Cells[, ] = "电话 传真";
  55. xSt.Cells[, ] = "客户对账单";
  56. xSt.Cells[, ] = "操作日期:" + DateTime.Now.ToLongTimeString();
  57.  
  58. //
  59. xSt.get_Range(xSt.Cells[, ], xSt.Cells[, ]).Font.Bold = true;
  60. xSt.get_Range(xSt.Cells[, ], xSt.Cells[, ]).Font.Size = ;
  61. xSt.get_Range(xSt.Cells[, ], xSt.Cells[, ]).Font.Bold = true;
  62.  
  63. xSt.get_Range(xSt.Cells[, ], xSt.Cells[, ]).Font.Bold = true;
  64.  
  65. //
  66. for (int i = ; i < ; i++)
  67. {
  68. xSt.get_Range(xSt.Cells[i, ], xSt.Cells[i, colIndex]).Select();
  69. xSt.get_Range(xSt.Cells[i, ], xSt.Cells[i, colIndex]).HorizontalAlignment = MSExcel.XlHAlign.xlHAlignCenterAcrossSelection;
  70. }
  71.  
  72. //
  73. xSt.get_Range(xSt.Cells[, ], xSt.Cells[rowIndex, colIndex]).Borders.LineStyle = ;
  74. xSt.get_Range(xSt.Cells[, ], xSt.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = MSExcel.XlBorderWeight.xlThick;
  75. xSt.get_Range(xSt.Cells[, ], xSt.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = MSExcel.XlBorderWeight.xlThick;
  76. xSt.get_Range(xSt.Cells[, ], xSt.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = MSExcel.XlBorderWeight.xlThick;
  77. xSt.get_Range(xSt.Cells[, ], xSt.Cells[rowIndex, colIndex]).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = MSExcel.XlBorderWeight.xlThick;
  78.  
  79. excel.Visible = true;
  80. string file = "e:/testexcel.xlsx";
  81. xBk.SaveCopyAs(file);
  82. }
  83. }
  84. }

转载:C# Office 开发的更多相关文章

  1. Office 开发版本号与版本对应关系

    Office 开发版本号与版本对应关系: office97 : 8.0 office2000 : 9.0 officeXP(2002) : 10.0 office2003 : 11.0 office2 ...

  2. [转载]Android相关开发网站

    my: Android 开发官方文档国内镜像-踏得网: http://wear.techbrood.com/index.html 转载自: http://my.oschina.net/luforn/b ...

  3. 转载:做Java开发这一年 (火龙果软件)

    转载:http://www.uml.org.cn/success/201410205.asp 从去年到现在,从.NET转向Java开发(只是因为项目原因,绝对与平台好坏没有关系)差不多有一年的时间了. ...

  4. Office开发必备知识----为什么要释放非托管Com资源

    https://www.cnblogs.com/Charltsing/p/RealeaseComObject.html QQ:564955427 目前,国内Office插件开发的风头正盛,很多VBAe ...

  5. 【转载】Office软件自定义功能区不完全显示修复方法

    转载地址:http://www.doudouxitong.net/guzhang/xitongjiqiao/2015/0603/8822.html 豆豆系统 Office是比较常用的办公软件,我们也会 ...

  6. 转载-iOS SDK开发

    最近帮兄弟公司的做支付业务sdk,积累了 sdk 封装的经验!下面我会从零开始把我的 sdk 封装和调试经历分享给大家,希望能给看到这篇文章的人有所帮助! 本文我会从以下几个方面来讲述: Framew ...

  7. [转载].NET Web开发技术(补充)

    大家在工作应该养成善于总结的习惯,总结你所学习.使用的技术,总结你所工作事项的比较好的地方,善于总结不断的沉淀优化自己.适时停下来总结下过去走过的路,才能让我们的未来走的更坚定.文章转自JamesLi ...

  8. 【转载】Web开发技术发展历史-版本1

    原文在这里. Web开发技术发展历史 Web的诞生 提到Web,不得不提一个词就是“互联网”.Web是World Wide Web的简称,中文译为万维网.“万维网”和我们经常说的“互联网”是两个联系极 ...

  9. (转载) 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 ...

随机推荐

  1. 浅析jquery中attr属性和prop属性的区别

    最近在做项目的时候,发现到了prop这个属性,然后之前一直使用的是attr属性,觉得感觉上都差不多,jQuery也不可能专门做了两个相同的属性撒.所以就结合这两个属性研究了一下,也谈谈我对他们最简单最 ...

  2. iOS 自我检測

    1.id 和 NSObject的差别? 2.UITableViewCell的复用原理? 3.UIView生命周期和UILayer的差别? 4.多线程NSOperation和Queue.GDC.Thre ...

  3. C++中函数的默认参数

    使用方法: (1)在函数声明或定义时,直接对参数赋值,该参数就是默认参数. (2)在函数调用时,省略部分或全部参数,这时就会使用默认参数进行代替. 注意事项: (1)一般在声明函数是设置默认参数. 如 ...

  4. 解决Win7&Win8 64位下Source Insight提示未完整安装的问题

    网上的破解版的注册表文件都是针对32位系统的,所以在64位系统里运行根本无法破解.下面分别贴出这俩系统里的破解文件. 使用方法: 分别复制对应系统的内容,新建文本文档,将内容粘贴进去,重命名为.reg ...

  5. 树形控件CTreeCtrl的使用

    树形控件在界面编程中应用十分普遍,如在资源管理器中和树形结构显示书的文件夹等,我们一步步研究树形控件的使用. 在对话框界面上首先拖动创建一个树,一般我们改变三个属性: Has Buttons显示带有& ...

  6. Fault Diagnosability Infrastructure Overview

    Fault Diagnosability Infrastructure Overview The fault diagnosability infrastructure aids in prevent ...

  7. asp.net application

    Application 对象用于存储和访问来自任何页面的变量,类似于 session 对象.不同之处在于,所有的用户分享一个 Application 对象,而 session 对象和用户的关系是一一对 ...

  8. android欢迎页源码

    直接上源码: import android.app.Activity; import android.content.Intent; import android.os.Bundle; import ...

  9. hdu1867A + B for you again

    Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...

  10. 初识Linux 命令

    1.linux基本原则 (1)有目的单一的小程序组成:组合小程序完成复杂任务 (2)一切皆文件 (3)尽量避免捕获用户接口 (4)配置文件保存为纯文本格式 2.命令格式 命令 选项 参数 短选项 - ...