处理Excel的值
在以前弄Asp.Net的时候就遇到一个Excel的问题,如果用ODBC等数据库驱动去读Excel的值时,在做类型判断的时候的行为蛮奇怪,当时写了一篇blog来记录的,翻了下找不到了,大体意思还是记得的.
这个时候可能会用到下面的VBScript来强行加一个"'"来处理一下."'"的作用是强行转换为文本.
这个时候可能会用到下面的VBScript来强行加一个"'"来处理一下."'"的作用是强行转换为文本.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim workSheetsCount As Integer
Dim workSheetRowsCount As Integer
Dim workSheetColumnCount As Integer
Dim strValue As String
Dim Worksheet As Worksheet
Dim Workbook As Workbook
Set Workbook = Workbooks(1)
For workSheetsCount = 1 To Workbook.Worksheets.Count
Set Worksheet = Workbook.Worksheets(workSheetsCount)
For workSheetRowsCount = 1 To Worksheet.UsedRange.Cells.Rows.Count
For workSheetColumnCount = 1 To Worksheet.UsedRange.Cells.Columns.Count
strValue = Worksheet.Cells(workSheetRowsCount, workSheetColumnCount)
Worksheet.Cells(workSheetRowsCount, workSheetColumnCount) = "'" + strValue
Next
Next
Next
End Sub
用惯了C/C++的语法习惯,用VBScript还真不习惯,写个For循环都用了半天,ft!Dim workSheetsCount As Integer
Dim workSheetRowsCount As Integer
Dim workSheetColumnCount As Integer
Dim strValue As String
Dim Worksheet As Worksheet
Dim Workbook As Workbook
Set Workbook = Workbooks(1)
For workSheetsCount = 1 To Workbook.Worksheets.Count
Set Worksheet = Workbook.Worksheets(workSheetsCount)
For workSheetRowsCount = 1 To Worksheet.UsedRange.Cells.Rows.Count
For workSheetColumnCount = 1 To Worksheet.UsedRange.Cells.Columns.Count
strValue = Worksheet.Cells(workSheetRowsCount, workSheetColumnCount)
Worksheet.Cells(workSheetRowsCount, workSheetColumnCount) = "'" + strValue
Next
Next
Next
End Sub