一、Domino中的函数
Private Const CLASS_NAME = "HandleCSVFile"
Private objLog As StdLog
Public Class HandleCSVFile
Private db As NotesDatabase
Private sess As NotesSession
Private ws As NotesUIWorkspace
''Private strFullPath As String
Sub new()
On Error Goto ErrHandler
Const PROC_NAME = "New"
WriteLog PROC_NAME, PROC_START, DEBUG_LOG
Set sess = New NotesSession
Set db = sess.CurrentDatabase
Set ws = New NotesUIWorkspace
ExitPoint:
WriteLog PROC_NAME, PROC_END, DEBUG_LOG
Exit Sub
ErrHandler:
WriteLog PROC_NAME, "Line " & Cstr(Erl) & " - " & Error$ & " (" & Cstr(Err) & ")" , ERROR_LOG
Resume ExitPoint
End Sub
Public Function ImportCSVToMappingProfile(strFilePath As String)
On Error Goto ErrHandler
Const PROC_NAME = "ImportCSVToMappingProfile"
WriteLog PROC_NAME, PROC_START, DEBUG_LOG
Dim fileNum As Integer
Dim strLineData As String
Dim vntLineData As Variant
Dim counter As Integer
Dim docMappingProfile As NotesDocument
Dim dcMapping As NotesDocumentCollection
Dim docMapping As NotesDocument
Dim strFormula As String
Dim strMachineID As String
Dim strOctopusID As String
fileNum = Freefile()
counter = 0
Open strFilePath For Input As fileNum%
Do While Not Eof(fileNum%)
Line Input #fileNum%, strLineData$
vntLineData = SplitCSVValue(strLineData$) ''Machine ID + Octopus ID
counter = counter + 1
''rule out the first line
If counter > 1 Then
If vntLineData(0) <> "" And vntLineData(1) <> "" Then
strMachineID = vntLineData(0)
strOctopusID = vntLineData(1)
strFormula = {(Form="MappingProfile") & (MachineID="} + strMachineID + {")}
Set dcMapping = db.Search(strFormula,Nothing,0)
If dcMapping.Count > 0 Then
Call dcMapping.StampAll("OctopusID",strOctopusID)
WriteLog PROC_NAME, "Mapping Profile: Update Octopus ID to '" + strOctopusID + "' depending on Machine ID '"+strMachineID+"'" , INFORMATION_LOG
Else
Set docMappingProfile = New NotesDocument(db)
With docMappingProfile
.Form = "MappingProfile"
.FormType = "MappingProfile"
.MachineID = strMachineID
.OctopusID = strOctopusID
End With
Call docMappingProfile.Save(True,False)
WriteLog PROC_NAME, "Create a new Mapping Profile, Machine ID is '" + strMachineID + "', Octopus ID is '"+strOctopusID+"'" , INFORMATION_LOG
End If
End If
End If
Loop
Msgbox "Import successfully.",48,"Ricoh"
ExitPoint:
WriteLog PROC_NAME, PROC_END, DEBUG_LOG
Exit Function
ErrHandler:
WriteLog PROC_NAME, "Line " & Cstr(Erl) & " - " & Error$ & " (" & Cstr(Err) & ")" , ERROR_LOG
Resume ExitPoint
End Function
Public Function ExportMappingProfileToCSV()
On Error Goto ErrHandler
Const PROC_NAME = "ExportMappingProfileToCSV"
WriteLog PROC_NAME, PROC_START, DEBUG_LOG
Dim fileName As Variant
Dim fileNum As Integer
Dim strMachineID As String
Dim strOctopusID As String
Dim dcMapping As NotesDocumentCollection
Dim docMapping As NotesDocument
Dim strFormula As String
Dim i As Integer
fileName = ws.SaveFileDialog(False,"Save Directory","CSV|*.csv","c:\","MappingProfile.csv")
If Not(Isempty(fileName)) Then
fileNum% = Freefile()
strFormula = {(Form="MappingProfile")}
Set dcMapping = db.Search(strFormula,Nothing,0)
If dcMapping.Count > 0 Then
Open fileName(0) For Output As fileNum%
Print #fileNum%, "Machine ID" + "," + "Octopus ID"
For i = 1 To dcMapping.Count
Set docMapping = dcMapping.GetNthDocument(i)
strMachineID = docMapping.GetItemValue("MachineID")(0)
strOctopusID = docMapping.GetItemValue("OctopusID")(0)
If Instr(strMachineID,"""") > 0 Then
strMachineID = Replace(strMachineID,"""","""""")
End If
If Instr(strOctopusID,"""") > 0 Then
strOctopusID = Replace(strOctopusID,"""","""""")
End If
If Instr(strMachineID,",") > 0 Then
strMachineID = {"} + strMachineID + {"}
End If
If Instr(strOctopusID,",") > 0 Then
strOctopusID = {"} + strOctopusID + {"}
End If
Print #fileNum%, strMachineID + "," + strOctopusID
Next
Close fileNum%
WriteLog PROC_NAME, "Export all mapping profile to file '" + Cstr(fileName(0)) + "'", INFORMATION_LOG
End If
Msgbox "Export successfully.",48,"Ricoh"
End If
ExitPoint:
WriteLog PROC_NAME, PROC_END, DEBUG_LOG
Exit Function
ErrHandler:
WriteLog PROC_NAME, "Line " & Cstr(Erl) & " - " & Error$ & " (" & Cstr(Err) & ")" , ERROR_LOG
Resume ExitPoint
End Function
Private Function SplitCSVValue(Byval LineStrData As String) As Variant
On Error Goto ErrHandler
Const PROC_NAME = "SplitCSVValue"
WriteLog PROC_NAME, PROC_START, DEBUG_LOG
Dim vntReturn As Variant
If Instr(LineStrData, ",") > 0 Then
vntReturn = SplitValue(LineStrData)
Else
Redim vntReturn(0)
vntReturn(0) = LineStrData
End If
SplitCSVValue = vntReturn
ExitPoint:
WriteLog PROC_NAME, PROC_END, DEBUG_LOG
Exit Function
ErrHandler:
WriteLog PROC_NAME, "Line " & Cstr(Erl) & " - " & Error$ & " (" & Cstr(Err) & ")" , ERROR_LOG
Resume ExitPoint
End Function
Private Function SplitValue(Byval OrgValue As String) As Variant
On Error Goto ErrHandler
Const PROC_NAME = "SplitValue"
WriteLog PROC_NAME, PROC_START, DEBUG_LOG
Dim vntTmp As Variant
Dim strTmp As String
Dim strNotSplit As String
Dim intLastCommaPos As Integer
Dim intPreCommaPos As Integer
Dim intTmpPos As Integer
Redim vntTmp(0)
strNotSplit = OrgValue
intPreCommaPos = 1
While strNotSplit <> ""
intLastCommaPos = Instr(intPreCommaPos, strNotSplit, ",")
If intLastCommaPos = 0 Then
If IsEvenQuotation(strNotSplit) Then
vntTmp(Ubound(vntTmp)) = FormatStr(strNotSplit)
Else
vntTmp(Ubound(vntTmp)) = strNotSplit
End If
End If
If intLastCommaPos > 0 Then
strTmp = Mid(strNotSplit, 1, intLastCommaPos - 1)
If IsEvenQuotation(strTmp) Then
vntTmp(Ubound(vntTmp)) = FormatStr(strTmp)
Redim Preserve vntTmp(Ubound(vntTmp) + 1)
intPreCommaPos = 1
strNotSplit = Mid(strNotSplit, intLastCommaPos + 1)
Else
intPreCommaPos = intLastCommaPos + 1
End If
Else
strNotSplit = ""
End If
Wend
SplitValue = vntTmp
ExitPoint:
WriteLog PROC_NAME, PROC_END, DEBUG_LOG
Exit Function
ErrHandler:
WriteLog PROC_NAME, "Line " & Cstr(Erl) & " - " & Error$ & " (" & Cstr(Err) & ")" , ERROR_LOG
Resume ExitPoint
End Function
Private Function IsEvenQuotation(Byval strTmp As String) As Boolean
On Error Goto ErrHandler
Const PROC_NAME = "IsEvenQuotation"
WriteLog PROC_NAME, PROC_START, DEBUG_LOG
Dim intQuotationCount As Integer
Dim intStart As Integer
intQuotationCount = 0
intStart = 0
IsEvenQuotation = True
While Instr(intStart + 1, strTmp, """") > 0
intStart = Instr(intStart + 1, strTmp, """")
intQuotationCount = intQuotationCount + 1
Wend
If intQuotationCount Mod 2 <> 0 Then
IsEvenQuotation = False
End If
ExitPoint:
WriteLog PROC_NAME, PROC_END, DEBUG_LOG
Exit Function
ErrHandler:
WriteLog PROC_NAME, "Line " & Cstr(Erl) & " - " & Error$ & " (" & Cstr(Err) & ")" , ERROR_LOG
Resume ExitPoint
End Function
Private Function FormatStr(Byval strTmp As String) As String
On Error Goto ErrHandler
Const PROC_NAME = "FormatStr"
WriteLog PROC_NAME, PROC_START, DEBUG_LOG
Dim strReturn As String
strReturn = strTmp
If Left(strReturn, 1) = """" Then
strReturn = Mid(strReturn, 2)
strReturn = Mid(strReturn, 1, Len(strReturn) - 1)
End If
FormatStr = Replace(strReturn, String(2, """"), """")
ExitPoint:
WriteLog PROC_NAME, PROC_END, DEBUG_LOG
Exit Function
ErrHandler:
WriteLog PROC_NAME, "Line " & Cstr(Erl) & " - " & Error$ & " (" & Cstr(Err) & ")" , ERROR_LOG
Resume ExitPoint
End Function
Private Function WriteLog(Byval Source As String, Byval LogMessage As String, LogLevel As Integer) As Integer
On Error Goto ErrHandler
Const PROC_NAME = "WriteLog"
WriteLog = False
If objLog Is Nothing Then
Set objLog = CreateLogObject()
End If
objLog.WriteLog CLASS_NAME & "\" & Source, LogMessage, LogLevel
WriteLog = True
Exit Function
ErrHandler:
Msgbox CLASS_NAME & "\" & Source & ": " & LogMessage, 48, CLASS_NAME
Exit Function
End Function
End Class
二、C#处理函数
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Common
{
public class HandlecCSVFile
{
public HandlecCSVFile()
{
}
public string[] SplitCSVLine(string lineData)
{
ArrayList columnsValue;
if (lineData.IndexOf(',') != -1)
{
columnsValue = SplitLine(lineData);
}
else
{
columnsValue = new ArrayList();
columnsValue.Add(lineData);
}
return (string[])columnsValue.ToArray(typeof(string));
}
private ArrayList SplitLine(string originalValue)
{
ArrayList columnsValue;
string notSplit;
int preCommaPos;
int lastCommaPos;
char splitBy;
string temp;
notSplit = originalValue;
columnsValue = new ArrayList();
splitBy = ',';
columnsValue.Add("");
preCommaPos = 0;
while (notSplit != string.Empty)
{
lastCommaPos = notSplit.IndexOf(splitBy, preCommaPos);
if (lastCommaPos == -1)
{
if (IsEvenQuotation(notSplit))
{
columnsValue[columnsValue.Count-1] = FormatStr(notSplit);
}
else
{
columnsValue[columnsValue.Count-1] = notSplit;
}
}
if (lastCommaPos > -1)
{
temp = notSplit.Substring(0, lastCommaPos);
if (IsEvenQuotation(temp))
{
columnsValue[columnsValue.Count - 1] = FormatStr(temp);
columnsValue.Add("");
preCommaPos = 0;
notSplit = notSplit.Substring(lastCommaPos + 1);
}
else
{
preCommaPos = lastCommaPos + 1;
}
}
else
{
notSplit = string.Empty;
}
}
return columnsValue;
}
private bool IsEvenQuotation(string tempString)
{
int quotationCount;
int start;
quotationCount = 0;
start = -1;
while (tempString.IndexOf('"', start+1) != -1)
{
start = tempString.IndexOf('"', start + 1);
quotationCount = quotationCount + 1;
}
if (quotationCount % 2 != 0)
{
return false;
}
else
{
return true;
}
}
private string FormatStr(string tempString)
{
string rtnString = tempString;
if (tempString.StartsWith("\"") == true)
{
rtnString = rtnString.Substring(1);
rtnString = rtnString.Substring(0, rtnString.Length - 1);
}
return rtnString.Replace("\"\"", "\"");
}
}
}