zoukankan      html  css  js  c++  java
  • C#使用office相关com组件

    添加引用 com组件(mso2003是11.0 mso2007应该是12)
    Microsoft Office 11.0 Object Library
    Microsoft Excel 11.0 Object Library

    名字空间
    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.Excel;

    声明变量

    1     private ApplicationClass m_App;
    2 public Workbook m_Wb;

    进程(对应Excel.exe)和工作簿(对于*.xls文件)

    1         m_App = new ApplicationClass();
    2 // m_App.Visible = true;
    3 m_Wb = m_App.Workbooks.Open(filePath,
    4 Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    5 Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    6 Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    7 Type.Missing, Type.Missing);

    关闭工作簿和进程

    1         m_App.Workbooks.Close();
    2 if(m_Wb != null)
    3 System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Wb);
    4
    5 m_App.Quit();
    6 if(m_Wb != null)
    7 System.Runtime.InteropServices.Marshal.ReleaseComObject(m_App);
    8 GC.Collect();

    获取Sheet

     1         string absFilePath="";
    2 XlsMan xls = null;
    3 if(FileUpload1.HasFile){
    4 try
    5 {
    6 absFilePath = Server.MapPath("xlsFiles\\") + Session["sguid"] + ".xls";
    7 FileUpload1.SaveAs(absFilePath);
    8
    9 tb.Text += FileUpload1.PostedFile.FileName + "\n";
    10 tb.Text += "数据量:" + FileUpload1.PostedFile.ContentLength + "kb\n";
    11 tb.Text += "类型:" + FileUpload1.PostedFile.ContentType + "\n";
    12
    13 xls = new XlsMan();
    14 xls.Init(absFilePath);
    15 Worksheet ws;
    16 ddlSheet.Items.Clear();
    17 for(int n = 1; n <= xls.m_Wb.Sheets.Count; n++){
    18 ws = (Worksheet)xls.m_Wb.Sheets[n];
    19 ddlSheet.Items.Add(new ListItem("工作表:" + ws.Name, ws.Name));
    20 }
    21 }
    22 catch(Exception ex)
    23 {
    24 tb.Text += "ERROR: " + ex.Message.ToString() + "\n";
    25 }
    26 finally
    27 {
    28 if (xls != null)
    29 xls.Close();
    30 }
    31 }

    获取内容

     1 XlsMan xls = null;
    2 try
    3 {
    4 string absFilePath = Server.MapPath("xlsFiles\\") + Session["sguid"] + ".xls";
    5 xls = new XlsMan();
    6 xls.Init(absFilePath);
    7 Worksheet ws = (Worksheet)xls.m_Wb.Sheets[ddlSheet.SelectedValue];
    8 int m, n;
    9 tb.Text += "--- Content: " + ws.Name + " ---\n";
    10 for (m = 1; m <= 3; m++)
    11 {
    12 for (n = 1; n <= 3; n++)
    13 {
    14 Range cell = (Range)ws.Cells[m, n];
    15 tb.Text += cell.Value2 + " ";
    16 }
    17 tb.Text += "\n";
    18 }
    19 }
    20 catch (Exception ex)
    21 {
    22 tb.Text += "ERROR: " + ex.Message.ToString() + "\n";
    23 }
    24 finally
    25 {
    26 if(xls != null)
    27 xls.Close();
    28 }

    如果是ASP.NET

    管理工具 组件服务 计算机 我的电脑 DCOM配置 Microsoft Excel应用程序(右键属性) 安全(访问权限和启动激活权限加入Network Service的本地启动激活权限) 标识(交互式用户)

    如果IIS建立

    加入NETWORK SERVICE的修改权限(因为目录需要上传文件用户需要读写服务器硬盘资源)

  • 相关阅读:
    实现分享功能(分享到qq空间,新浪微博)
    AXIOS构建请求处理全局loading状态&&AXIOS避免重复请求loading多次出现
    Vue.use() 方法
    判断浏览器版本
    判断当前环境是ios还是安卓
    如何理解react中的super() super(props)
    JavaScript 函数调用时带括号和不带括号的区别
    npm 安装时 --save --dev 和 --save 区别
    npm 全局安装和局部安装的区别
    module.exports 与 exports
  • 原文地址:https://www.cnblogs.com/cheetahw/p/2185430.html
Copyright © 2011-2022 走看看