C#读写EXCEL源码提示“office检测到此文件存在一个问题。为帮助保护您的计算机,不能打开此文件。 ”的解决
我发现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;//设置不显示确认修改提示//创建并写入数据到sheettry{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的Namesheet.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); //创建一张表,一张表可以包含多个sheetxls.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();}}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://dhexx.cn/news/show-6487926.html
如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网进行投诉反馈,一经查实,立即删除!