C#读写EXCEL源码提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件。 ”的解决

来源:互联网 发布:淘宝店铺提高流量 编辑:程序博客网 时间:2024/05/21 22:29


我发现excel最新的扩展名.xlsx不用打开文件在计算机里就可以直接搜里面的内容。但老的.xls就搜不到。


1.C#写的操作EXCEL应用 提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件。 之前还正常,换个电脑就报错。

先手工打开Excel,会提示“受保护的视图”,不能编辑。点启用编辑就行了。再打开软件,正常了。


2.找不到引用microsoft.office.core

在项目引用中右击选择添加引用,选择COM里面选择Microft Office 12.0 object Library和Microft Excel 12.0 object Library分别点确定即可!同样如果要引用World选Microft World 12.0 object Library!  2003/2007共通处理方式 分别为11或12版本  添加.net中Microsoft.Office.Interop.excel;  添加.net中Office 


3.源码示例

using System;using System.Reflection;using System.IO;using Microsoft.Office.Interop.Excel;namespace Excel{    class Excel    {        public static int startRow =5;        public static string jzmc = "";        public static string id = "";        public static Double count = 0;        public static Array zw;        public static bool WriteXls(string filename, System.Collections.ArrayList rowData, int sheetNum)        {            //启动Excel应用程序            Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();          //  _Workbook book = xls.Workbooks.Add(Missing.Value); //创建一张表,一张表可以包含多个sheet            //如果表已经存在,可以用下面的命令打开            _Workbook book = xls.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);            _Worksheet sheet;//定义sheet变量            xls.Visible = false;//设置Excel后台运行            xls.DisplayAlerts = false;//设置不显示确认修改提示            //创建并写入数据到sheet            try            {                sheet = (_Worksheet)book.Worksheets.get_Item(sheetNum);//获得第i个sheet,准备写入            }            catch (Exception ex)//不存在就增加一个sheet            {                sheet = (_Worksheet)book.Worksheets.Add(Missing.Value, book.Worksheets[book.Sheets.Count], 1, Missing.Value);            }            //sheet.Name = "第" + i.ToString() + "页";//设置当前sheet的Name            sheet.get_Range("C3", Type.Missing).Cells.Value2=jzmc;            sheet.get_Range("G3", Type.Missing).Cells.Value2=id;            sheet.get_Range("I3", Type.Missing).Cells.Value2=count;                       for (int offset = 0; offset < 10; offset++)            {                string str = rowData[offset].ToString();                sheet.Cells[startRow, offset + 1] = rowData[offset].ToString();                            }            startRow++;//行位置向前加                         //将表另存为          //  book.SaveAs(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);            //如果表已经存在,直接用下面的命令保存即可            book.Save();            book.Close(false, Missing.Value, Missing.Value);//关闭打开的表            xls.Quit();//Excel程序退出            //sheet,book,xls设置为null,防止内存泄露            sheet = null;            book = null;            xls = null;            GC.Collect();//系统回收资源            return true;        }        public static bool CreateFile(string filename)        {            try            {                //启动Excel应用程序                Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();                _Workbook book = xls.Workbooks.Add(Missing.Value); //创建一张表,一张表可以包含多个sheet                xls.DisplayAlerts = false;//设置不显示确认修改提示                //将表另存为                book.SaveAs(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);                book.Close(false, Missing.Value, Missing.Value);//关闭打开的表                xls.Quit();//Excel程序退出                //sheet,book,xls设置为null,防止内存泄露                xls = null;                GC.Collect();//系统回收资源            }            catch (Exception ex)//不存在就退出            {                return false;            }            return true;        }        public static Array ReadXls(string filename, int index)//读取第index个sheet的数据        {            //启动Excel应用程序            Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();            //打开filename表            _Workbook book = xls.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);             try            {                    _Worksheet sheet;//定义sheet变量                    xls.Visible = false;//设置Excel后台运行                    xls.DisplayAlerts = false;//设置不显示确认修改提示                                  sheet = (_Worksheet)book.Worksheets.get_Item(index);//获得第index个sheet,准备读取                              Console.WriteLine(sheet.Name);                    int row = sheet.UsedRange.Rows.Count;//获取不为空的行数                    int col = sheet.UsedRange.Columns.Count;//获取不为空的列数                    jzmc = sheet.get_Range("C3", Type.Missing).Cells.Value2;                    id = sheet.get_Range("G3", Type.Missing).Cells.Value2;                    count = sheet.get_Range("I3", Type.Missing).Cells.Value2;                    zw = (Array)sheet.get_Range("B5", "B" + row).Cells.Value2;  //获得区域数据赋值给Array数组,方便读取                    Array value = (Array)sheet.get_Range("A5", "F" + row).Cells.Value2;  //获得区域数据赋值给Array数组,方便读取                    // Array value = (Array)sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[row, col]).Cells.Value2;                    book.Save();//保存                    book.Close(false, Missing.Value, Missing.Value);//关闭打开的表                    xls.Quit();//Excel程序退出                    //sheet,book,xls设置为null,防止内存泄露                    sheet = null;                    book = null;                    xls = null;                    GC.Collect();//系统回收资源                    return value;               }             catch (Exception ex)//不存在就退出             {                 book.Close(false, Missing.Value, Missing.Value);//关闭打开的表                 xls.Quit();//Excel程序退出                 //sheet,book,xls设置为null,防止内存泄露                                book = null;                 xls = null;                 GC.Collect();//系统回收资源                 return null;             }                            }        public static void Copy(string path, string path2)        {            //string path = @"c:/temp/MyTest.txt";           // string path2 = path + "temp";            try            {                // Create the file and clean up handles.               // using (FileStream fs = File.Create(path)) { }                // Ensure that the target does not exist.                File.Delete(path2);                // Copy the file.              //  File.Copy(path, path2);              //  Console.WriteLine("{0} copied to {1}", path, path2);                // Try to copy the same file again, which should succeed.                File.Copy(path, path2, true);                             //  Console.WriteLine("The second Copy operation succeeded, which was expected.");            }            catch            {               // Console.WriteLine("Double copy is not allowed, which was not expected.");            }        }       public static void Test()        {            string Current;            Current = Directory.GetCurrentDirectory();//获取当前根目录            Array Data = ReadXls(Current + "\\JMO-PCBA-019.xls", 1);//读取test.xls的第一个sheet表            foreach (string temp in Data)                Console.WriteLine(temp);            Console.ReadKey();        }    }}


阅读全文
0 0