我用visual C#.NET打开一个excel,它显示为可读文件,如何使打开的文件为可读写??就是普通的模式。
因为我要对打开的excel进行操作,并要将操作结果记录到数据库中,现在文件打开方式为只读,我的操作无法进行,请知道的朋友帮我一把。下面是我打开excel的代码,请问要如何...
因为我要对打开的excel进行操作,并要将操作结果记录到数据库中,现在文件打开方式为只读,我的操作无法进行,请知道的朋友帮我一把。下面是我打开excel的代码,请问要如何修改呢?
Excel.Application xApp = new Excel.ApplicationClass();
xApp.Visible = true;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
Excel.Workbook xBook = xApp.Workbooks._Open(@"F:\excel\1.xls",
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value); 展开
Excel.Application xApp = new Excel.ApplicationClass();
xApp.Visible = true;
//得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件
Excel.Workbook xBook = xApp.Workbooks._Open(@"F:\excel\1.xls",
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value); 展开
3个回答
展开全部
我也正在做这个, 下面是我写的导入方法,传入路径,返回dataset ,已经实现。希望对你有帮助。
public DataSet ImportExcel(string fileName)
{
//判断是否安装EXCEL
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
if (xlApp == null)
{
Page.ClientScript.RegisterStartupScript(GetType(),"z","<script>alert('无法创建Excel对象,可能您的计算机未安装Excel!')</script>");
return null;
}
//判断文件是否被其他进程使用
Microsoft.Office.Interop.Excel.Workbook workbook;
try
{
workbook = xlApp.Workbooks.Open(fileName, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);
}
catch
{
Page.ClientScript.RegisterStartupScript(GetType(),"z","<script>alert('Excel文件处于打开状态,请保存关闭')</script>");
return null;
}
//获得所有Sheet名称
int n = workbook.Worksheets.Count;
string[] SheetSet = new string[n];
System.Collections.ArrayList al = new System.Collections.ArrayList();
for (int i = 1; i <= n; i++)
{
SheetSet[i - 1] = ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[i]).Name;
}
//释放Excel相关对象
workbook.Close(null, null, null);
xlApp.Quit();
if (workbook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
}
if (xlApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
GC.Collect();
//把EXCEL导入到DataSet
DataSet ds = new DataSet();
string connStr = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + fileName + ";Extended Properties=Excel 8.0";
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
OleDbDataAdapter da;
for (int i = 1; i <= n; i++)
{
string sql = "select * from [" + SheetSet[i - 1] + "$] ";
da = new OleDbDataAdapter(sql, conn);
da.Fill(ds, SheetSet[i - 1]);
da.Dispose();
}
conn.Close();
conn.Dispose();
}
//DataRow[] dr = new DataRow[ds.Tables[0].Rows.Count];
ds.Tables[0].Rows.RemoveAt(ds.Tables[0].Rows.Count - 1);
return ds;
}
public DataSet ImportExcel(string fileName)
{
//判断是否安装EXCEL
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
if (xlApp == null)
{
Page.ClientScript.RegisterStartupScript(GetType(),"z","<script>alert('无法创建Excel对象,可能您的计算机未安装Excel!')</script>");
return null;
}
//判断文件是否被其他进程使用
Microsoft.Office.Interop.Excel.Workbook workbook;
try
{
workbook = xlApp.Workbooks.Open(fileName, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, 1, 0);
}
catch
{
Page.ClientScript.RegisterStartupScript(GetType(),"z","<script>alert('Excel文件处于打开状态,请保存关闭')</script>");
return null;
}
//获得所有Sheet名称
int n = workbook.Worksheets.Count;
string[] SheetSet = new string[n];
System.Collections.ArrayList al = new System.Collections.ArrayList();
for (int i = 1; i <= n; i++)
{
SheetSet[i - 1] = ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[i]).Name;
}
//释放Excel相关对象
workbook.Close(null, null, null);
xlApp.Quit();
if (workbook != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
}
if (xlApp != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
GC.Collect();
//把EXCEL导入到DataSet
DataSet ds = new DataSet();
string connStr = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + fileName + ";Extended Properties=Excel 8.0";
using (OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
OleDbDataAdapter da;
for (int i = 1; i <= n; i++)
{
string sql = "select * from [" + SheetSet[i - 1] + "$] ";
da = new OleDbDataAdapter(sql, conn);
da.Fill(ds, SheetSet[i - 1]);
da.Dispose();
}
conn.Close();
conn.Dispose();
}
//DataRow[] dr = new DataRow[ds.Tables[0].Rows.Count];
ds.Tables[0].Rows.RemoveAt(ds.Tables[0].Rows.Count - 1);
return ds;
}
追问
我没有修改代码,可是打开excel莫名其妙的变成了可以编辑的状态,问题似乎解决了。但是现在我要打开word,word又变成了只读文档,如果你知道解决办法请告诉我,谢谢了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |