public static void ReWriteFile(string excelPath)
{
if(!System.IO.File.Exists(excelPath))return;
Excel.ApplicationClass myExcel=new Excel.ApplicationClass();
if(myExcel==null)
{
throw new Exception("启动Excel失败");
}
Excel.Workbooks workBooks=myExcel.Workbooks;
Excel.Workbook myExcelbook=workBooks.Open(excelPath,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing
,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
Excel.Sheets sheet=myExcelbook.Worksheets; Excel.Worksheet workSheet=(Excel.Worksheet)sheet.get_Item(1);
if(workSheet==null)
{
throw new Exception("没有工作Sheet");
}
for(int i=0;i<workSheet.Cells.Count;i++)
{
for(int j=0;j<workSheet.Columns.Count;j++)
{
if(workSheet.Cells[i,j].ToString().Trim()!=string.Empty)//这里
workSheet.Cells[i,j]="'"+workSheet.Cells[i,j];
}
}
if(!myExcelbook.Saved)
myExcelbook.Close(true,excelPath,true);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myExcel=null;
GC.Collect();
GC.WaitForPendingFinalizers();
}if(workSheet.Cells[i,j].ToString().Trim()!=string.Empty)//这里出错
workSheet.Cells[i,j]="'"+workSheet.Cells[i,j];错误是:
HRESULT 中的异常:0x800A03EC。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: HRESULT 中的异常:0x800A03EC。谢谢回复
http://blog.csdn.net/jinjazz/archive/2008/08/04/2766203.aspx
http://blog.csdn.net/jinjazz/archive/2008/08/05/2770632.aspx
http://blog.csdn.net/jinjazz/archive/2008/08/06/2775725.aspx循环是最差的方法
string MyExcel = this.textBox1.Text; string MySheet = this.textBox2.Text;
OleDbConnection MyConnection=new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+MyExcel+";Excel 8.0;HDR=YES");//注意不要换行
OleDbCommand MyCommand = new OleDbCommand("SELECT * FROM [" + MySheet + "$ ]", MyConnection);
//SQL在Excel里的使用格式: SELECT * FROM [Sheet1$A1:C10] :显示从A到C的1至10表格的所有数据
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(MyCommand);
DataSet MySet = new DataSet();
MyAdapter.Fill(MySet); this.dataGridView1.DataSource = MySet.Tables[0];// 从0开始索引
key1Xpos.Text =dataGridView1.Rows[0].Cells[0].Value.ToString();