我想修改现有的Excel文件。
以下代码在文档文件夹中生成重复的已修改excel文件,而不是修改原始文件。
注意:我要修改的文件不在.text(function (d, i) {
return i;
});
文件夹
Documents
答案 0 :(得分:2)
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Documents and Settings\crysol\Desktop\TEST\Book1.xls;Extended Properties=""Excel 12.0 Xml;HDR=YES""")
cn.Open()
With cm
.Connection = cn
.CommandText = "update [up$] set [name]=?, [QC_status]=?, [reason]=?, [date]=? WHERE [article_no]=?"
cm = New OleDbCommand(.CommandText, cn)
cm.Parameters.AddWithValue("?", TextBox2.Text)
cm.Parameters.AddWithValue("?", ComboBox1.SelectedItem)
cm.Parameters.AddWithValue("?", TextBox3.Text)
cm.Parameters.AddWithValue("?", DateTimePicker1.Text)
cm.Parameters.AddWithValue("?", TextBox1.Text)
cm.ExecuteNonQuery()
MsgBox("UPDATE SUCCESSFUL")
con.Close()
End With
答案 1 :(得分:0)
这里我已经展示了向现有的excel文件添加工作表并将数据写入新创建的工作表。
Try
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
'On Error GoTo Err_Handler
' Start Excel and get Application object.
oXL = New Excel.Application
' Get a new workbook.
Dim path As String = ViewState("filepath")
oWB = oXL.Workbooks.Open(path)
oSheet = CType(oXL.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value), Excel.Worksheet)
'oSheet.Name = "Reject_History"
Dim totalSheets As Integer = oXL.ActiveWorkbook.Sheets.Count
CType(oXL.ActiveSheet, Excel.Worksheet).Move(After:=oXL.Worksheets(totalSheets))
CType(oXL.ActiveWorkbook.Sheets(totalSheets), Excel.Worksheet).Activate()
'Write Dataset to Excel Sheet
Dim col As Integer = 0
For Each dr As DataColumn In DirectCast(ViewState("DisplayNonExisting"), DataTable).Columns
col += 1
'Determine cell to write
oSheet.Cells(10, col).Value = dr.ColumnName
Next
Dim irow As Integer = 10
For Each dr As DataRow In DirectCast(ViewState("DisplayNonExisting"), DataTable).Rows
irow += 1
Dim icol As Integer = 0
For Each c As String In dr.ItemArray
icol += 1
'Determine cell to write
oSheet.Cells(irow, icol).Value = c
Next
Next
' Make sure Excel is visible and give the user control
' of Microsoft Excel's lifetime.
' oXL.Visible = True
' oXL.UserControl = True
'oWB.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, False, False, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
'oWB.Close()
oWB.Save()
oWB.Close(Type.Missing, Type.Missing, Type.Missing)
' Make sure you release object references.
oRng = Nothing
oSheet = Nothing
oWB = Nothing
oXL = Nothing
Catch ex As Exception
End Try