插入数据后保存Excel文件时出现问题 [英] Problem saving excel file after inserting data

查看:108
本文介绍了插入数据后保存Excel文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据写入到现有的excel文件中(我很容易做到) 但是我无法将更改保存在excel文件上(实际上,我在excel文件上看到了更改,但是似乎已打开,并且毕竟发生了一些问题,例如该文件已经以相同的名称打开,等等... )

I want to write data to an existing excel file ( I do it easily ) But I can not save the changes on the excel file ( actually I see the changes on the excel file, but it seems opened and after all it occurs some problems such as "the file is already opened with same name and so on ... )

Excel.Application app= new Microsoft.Office.Interop.Excel.Application();
        Excel.Workbook appbook= app.Workbooks.Open(appxls, 0, true, 5, "", "", false, Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, Missing.Value, Missing.Value);
        Excel.Sheets pages= appbook.Worksheets;
        Excel.Worksheet page= (Excel.Worksheet)pages.get_Item(1);

//...我更改了excel文件中的一些值,并希望保存它们: //appxls是保存路径的字符串

//... i change some values on the excel file and want to save them : // appxls is a string holding the path

 appbook.SaveAs(appxls, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing,false, Type.Missing, Excel.XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    appbook.Close(true, Missing.Value, Missing.Value);
    app.Quit();

问题出在哪里,如何使用Microsoft.interop解决它.

Where is the problem, how can I solve it using Microsoft.interop.

推荐答案

您正在使用名为"appxls"的文件名作为只读文件来打开工作簿. (Workbooks.Open方法的ReadOnly参数是第三个参数,您要传入true.)

You are opening the workbook using the file name named 'appxls' as read-only. (The ReadOnly parameter of the Workbooks.Open method is the third parameter, and you are passing in true.)

您稍后将使用Workbook.SaveAs方法,但是尝试使用与打开工作簿时所用的"appxls"变量所保存的文件名完全相同的文件名来保存文件.这是尝试覆盖您已打开的只读文件的尝试,因此被阻止.

You are later using the Workbook.SaveAs method, but are attempting to save the file using the same exact file name held by your 'appxls' variable that you used to open the workbook. This is an attempt to overwrite the read-only file that you have open, and, therefore, is prevented.

我可以看到两种可能的解决方案:

I can see two possible solutions:

  1. 传入false作为Workbooks.Open方法的ReadOnly参数,以便以后可以使用Workbook.Save方法保存它.您可以通过调用appbook.Save()来保存它.

  1. Pass in false for the ReadOnly parameter of the Workbooks.Open method so that you can save it later using Workbook.Save method. In your case, you could save it by calling appbook.Save().

继续以只读方式打开文件,但是以后使用Workbook.SaveAs方法时,应将其保存为不同名称.

Continue to open the file as read-only, but when you later use the Workbook.SaveAs method, you should save it under a different name.

希望这会有所帮助!

这篇关于插入数据后保存Excel文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆