导入 EXECL 数据 和 特殊字符枚举
protected void btnCheck_Click(object sender, EventArgs e)
{
//ClearData();
if (!UploadFileHelper.IsExcel(Path.GetExtension(importFile.PostedFile.FileName)))
{
divMessage.ShowMessageError("只能上传Excel文件。");
}
string savePath = importFile.PostedFile.SaveAsGuidNameWithDatePath("~/TemplateFiles/Upload/");
string path = Server.MapPath("~/TemplateFiles/Upload/" + savePath);
DataSet ds = new DataSet();
try
{
OleDbDataAdapter adp = new OleDbDataAdapter("select * from [Sheet1$]"
, "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 8.0;");
adp.Fill(ds);
}
catch
{
divMessage.ShowMessageError("数据文件格式错误,请重新申报包含有效项目数据的Excel文件。");
btnImport.Visible = false;
return;
}
try { File.Delete(path); }
catch { }
int rowCount = 0;
int colCount = 0;
if (ds.Tables.Count > 0)
{
DataTable dt = ds.Tables[0];
dt.Columns.Add(new DataColumn() { ColumnName = "逻辑站名" });
dt.Columns.Add(new DataColumn() { ColumnName = "逻辑站号" });
rowCount = dt.Rows.Count;
colCount = dt.Columns.Count;
string phystationname;
string phystationnumber;
string networktype;
string logicname;
string logicnumber;
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
phystationname = dt.Rows[i][0].ToString();
phystationnumber = dt.Rows[i][1].ToString();
networktype = dt.Rows[i][4].ToString();
if (!string.IsNullOrEmpty(networktype))
{//Todo:添加异常捕获
try
{
getlogic(phystationname, phystationnumber, networktype, out logicname, out logicnumber);
dt.Rows[i][5] = logicname;
dt.Rows[i][6] = logicnumber;
}
catch (ApplicationException ex)
{
divMessage.ShowMessageWarn(ex.Message);
return;
}
}
if (string.IsNullOrEmpty(dt.Rows[i][0].ToString().Trim()) && string.IsNullOrEmpty(dt.Rows[i][1].ToString().Trim()))
dt.Rows.RemoveAt(i);
}
}
if (rowCount < 1)
{
divMessage.ShowMessageError("导入的Excel文件中没有数据行,请重新导入包含有效项目数据的Excel文件。");
return;
}
InfoList = new List<StationInfo>();
gv.DataSource = ds.Tables[0];
gv.DataBind();
divMessage.ShowMessageError(string.Format("共导入{0}行站点数据,{1}行站点数据通过验证。", rowCount, InfoList.Count(s => s.Flag == "√")));
btnImport.Visible = true;
divInfo.Visible = true;
}
二 ,枚举
public enum ExpenditureType
{
[EnumText("技改-在安装工程-主设备")]
技改在安装工程主设备 = 1,
[EnumText("技改-在安装工程-配套材料")]
技改在安装工程配套材料 = 2,
[EnumText("技改-在安装工程-其他")]
技改在安装工程其他 = 3
}