我正在开发一个电子表格上传工具,用于创建/更新联系人,这些工具将作为新选项添加到我的网站。
电子表格包含以下列:
数据库表结构如下:
Contact(contact_id INT PK IDENTITY(1,1), fullname VARCHAR(30), jobtitle VARCHAR(100), salutation VARCHAR(100), qualifications VARCHAR(100), companyname VARCHAR(100) , address1 VARCHAR(100), address2 VARCHAR(100), address3 VARCHAR(100))
规则是:
1. The Full name must contain at least one space (leading and/or trailing spaces will be trimmed).
2. Total length of the full name must not exceed 30 characters (including any space(s)).
3. 'Full Name' is the key field to identify an existing contact in the system. As such, raise a validation error if this field is
blank.
4. If a match is found on the 'Full Name', then the other fields of the contact will be updated with the values populated in the
spreadsheet.
5. If a match is not found on the key field, then create a new contact with the details.
6. When creating/updating a contact a value must be populated in at least one other column (in addition to the 'Full Name'), otherwise
raise a validation error.
过程:
有两个阶段称为“验证阶段”和“实际上传”阶段。 - 上传电子表格后,必须按照上述规则验证电子表格,并显示验证/状态消息(在屏幕上): 例如:
Row 1 is the header row so it will not be validated.
Row 2: Error - Full Name cannot be empty
Row 3: OK - A contact will be created
Row 4: OK - Contact will be updated
Row 5: Error - Full Name must contain at least one space
Row 6: Error - At least one other column must be populated in addition to the 'Full Name'
用户将有两个选项:
我已经成功完成了上面所说明的所有内容,但我必须在“实际上传”阶段重新验证每一行(即我正在进行两次相同的检查)。
我的问题是,有没有办法保留验证阶段的结果,所以我不必在第二阶段重新验证每条记录?
我使用.NET Framework (Version 4.0.30319.34209)
{和VB.NET/ASP.NET
使用VISUAL STUDIO
上的NativeExcel库来推动这一点。
请注意所有代码都将使用VB.NET
在代码隐藏页面中编写(我在这里别无选择,抱歉)。
非常感谢任何建议/帮助。
答案 0 :(得分:0)
将验证状态消息保存为List(Of String),然后将其作为会话变量,以便在上载阶段将其用作提示。
答案 1 :(得分:-2)
要阅读Excel文件: 右键单击引用> COM> Office xx.x对象应用程序
Microsoft.Office.Interop.Excel.Application exlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook exlWb = exlApp.Workbooks.Open(@"C:\Users\user\Excelfile.xls");
Microsoft.Office.Interop.Excel.Worksheet exlWs = exlWb.Sheets["Sheet1"];
int col = Convert.ToInt32(usedRange.Columns.Count);
int row = Convert.ToInt32(usedRange.Rows.Count);
exlApp.Visible = true;
string[,] cellValue = new string[row + 1, col + 1];
for (int j = 1; j <= row - 1; j++)
{
for (int k = 1; k <= col - 1; k++)
{
cellValue[j, k] = exlWs.Cells[j, k + 1].ToString();
}
}
exlWb.Close();
exlWs = null;
exlWb = null;
exlApp.Quit();
exlApp = null;