replace或更新Excel文件中的条目

我正尝试在上传的Excel(.xlsx)文件中清除一些数据(本例中为“电话号码”)。 我有以下代码来打开文件:

protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) try { string filePath = ("confirm//") + FileUpload1.FileName; FileUpload1.SaveAs(Server.MapPath(filePath)); Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application(); Workbook wb = xl.Application.Workbooks.Open(Server.MapPath(filePath)); wb.Activate(); string csvPath = (filePath.Replace(".xlsx", ".csv")); wb.SaveAs(Server.MapPath(csvPath), Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV); wb.Close(); // call method to parse csv ReadRec(csvPath); } catch (Exception ex) {//} else {//} } 

然后像这样的东西,如果不是已经存在的数字的开始添加一个零:

 private void ReadRec(string csvName) { StreamReader Sr = new StreamReader(Server.MapPath(csvName)); string s; while (!Sr.EndOfStream) { s = Sr.ReadLine(); string company = s.Split(',')[0]; string phone = s.Split(',')[1]; string NAME = s.Split(',')[2]; if (!phone.StartsWith("0")) { phone = "0" + phone; } } Sr.Close(); } 

这似乎工作得很好,但我还没有弄清楚如何重新插入更新的数字到电子表格(或创build一个新的Excel文件与更新的数据)。

任何人都可以给我一些指针?

好吧,最后拼凑出一种解决scheme(足以满足我当前的目的),但远非理想:

 protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) try { var excelApp = new Application(); excelApp.Workbooks.Open("C:\\myFile.xls", 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); var ws = excelApp.Worksheets; var worksheet = (Worksheet)ws.get_Item("Sheet1"); Range range = worksheet.UsedRange; object[,] values = (object[,])range.Value2; for (int row = 1; row <= values.GetUpperBound(0); row++) { string phone = Convert.ToString(values[row, 2]); if (!phone.StartsWith("0")) { phone = "0" + phone; } range.Cells.set_Item(row, 2, phone); } excelApp.Save("C:\\Leads.xls"); excelApp.Quit(); } catch (Exception ex) {//} else {//} } 

-EDIT-为了这个工作,我必须在Excel中打开.xlsx文件并将其保存为.xls

我使用这个代码来更新EXCEL表单中的数据

  try { cmd.CommandText = "UPDATE [Sheet1$] SET ID=@value,Name=@value2,Age=@value3 where ID=@value4"; cn.Open(); cmd.Parameters.AddWithValue("@value1", txtid.Text); cmd.Parameters.AddWithValue("@value2", txtname.Text); cmd.Parameters.AddWithValue("@value3", txtage.Text); cmd.Parameters.AddWithValue("@value4", txtupdate.Text); cmd.ExecuteNonQuery(); MessageBox.Show("Record Updated Successfully"); cn.Close(); } catch (Exception e3) { MessageBox.Show("Error" + e3); } 

我真的不使用c#或asp.net所以这(可能)不会是工作代码。

在C#中的Interop 应该非常简单,文档通常具有特定于C#的示例,因此如果失败,请查看MSDN。

 protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) try { string filePath = ("confirm//") + FileUpload1.FileName; FileUpload1.SaveAs(Server.MapPath(filePath)); Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application(); Workbook wb = xl.Application.Workbooks.Open(Server.MapPath(filePath)); wb.Activate(); Worksheet ws = wb.Worksheets(1); // use the first sheet 1-based index i think for (long row = 2; i <= ws.UsedRange.Rows.Count; i++) { // assume row 1 is a title so start at row 2 // ws.Cells(row, 1) // company // ws.Cells(row, 2) // phone // ws.Cells(row, 3) // name if (!ws.Cells(row,2).Value2.StartsWith("0") { ws.Cells(row,2).Value2 = "0" + ws.Cells(row,2).Value2 } } //string csvPath = (filePath.Replace(".xlsx", ".csv")); //wb.SaveAs(Server.MapPath(csvPath), // Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV); wb.Save(); // as were directly modifying the file, no need to save elsewhere wb.Close(); // call method to parse csv //ReadRec(csvPath); } catch (Exception ex) {//} else {//} }