如果excel文件打开,如何在写入流期间避免excel文件的只读,以便从C#中脱颖而出? [英] How can we avoid readonly of the excel file during the writestream to excel from C# , if the excel file is open?

查看:127
本文介绍了如果excel文件打开,如何在写入流期间避免excel文件的只读,以便从C#中脱颖而出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从c#向excel写入值时,在excel文件打开的同时显示文件处于readonly的错误,那么我们如何能够在从c#到Excel的写入流中避免该错误< br $> b $ b

我的尝试:



during writing values to an excel from c#, at the same time the excel file is open it shows the error that the file is in readonly , so how can we able to avoid that error during write stream from c# to the Excel

What I have tried:

 //Get all the sheets in the workbook
 mWorkSheets = mWorkBook.Worksheets;
 //Get the allready exists sheet
 mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Sheet1");
 Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;

 int colCount = range.Columns.Count;


 int rowCount = range.Rows.Count+1;
 for (int index = 0; index < NoOfRecords; index++)
 {
     for (int j = 0; j < colCount; j++)
     {                       
         mWSheet1.Cells[(rowCount) + index, j + 1] ="'"+Convert.ToString(ResultsData.Rows[index][j].ToString());
     }
 }


 mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
 Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
 Missing.Value, Missing.Value, Missing.Value,
 Missing.Value, Missing.Value);
mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);

推荐答案

如果文件被锁定通过另一个过程打开,你无能为力。您必须将内容写入不同的文件,或者您必须等到文件被其他进程关闭。
If the file is locked open by another process there is nothing you can do about it. You have to write the content to a different file or you have to wait until the file is closed by the other process.


引用:

在从c#向excel写入值时,同时excel文件打开,它显示文件处于readonly的错误,那么我们怎样才能在写入流时避免该错误c#到Excel

during writing values to an excel from c#, at the same time the excel file is open it shows the error that the file is in readonly , so how can we able to avoid that error during write stream from c# to the Excel



2个或更多Excel可以同时在同一个WorkBook上工作。工作簿必须由Excel共享处理。

我不知道Interop是否可以处理这个以及如何处理。


2 or more Excels can work on same WorkBook at a same time. The WorkBook must be handled as shared by Excel.
I don't know if Interop can handle this and how.


这篇关于如果excel文件打开,如何在写入流期间避免excel文件的只读,以便从C#中脱颖而出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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