62,243
社区成员




//先将Excel传到服务器上面
protected void Button1_Click(object sender, EventArgs e)
{
string Excel = string.Empty;
Boolean fileOK = false;
String path = Server.MapPath("~/UploadExcel/");
if (FileUpload1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions =
{ ".xls" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path
+ FileUpload1.FileName);
Excel = FileUpload1.PostedFile.FileName;
DataTableToDB(Excel);
//Label1.Text = "File uploaded!";
}
catch (Exception ex)
{
Response.Write(ex.Message);
Label1.Text = "上传失败!";
}
}
else
{
Label1.Text = "文件格式不对.";
}
}
public static void DataTableToDB(string Ex)
{
ASP.admin_bjproduct_list_aspx list = new admin_bjproduct_list_aspx();
BJProductCategory CC = new BJProductCategory();
DataTable dtExcel = ExcelToDataTable(Ex, "Sheet1");
DT = CreateParentTable();
try
{
for (int i = 0; i < dtExcel.Rows.Count; i++)
{
if (dtExcel.Rows[i][0].ToString() != null || dtExcel.Rows[i][0].ToString() != "")
{
DR = DT.NewRow();
int PID = CategoryID(dtExcel.Rows[i][2].ToString());
CC.ID = PID;
WebManager.GetInfo(CC);
if (CC.Title == "")
{
continue;
}
else
{
DR["Title"] = dtExcel.Rows[i][1].ToString();
DR["ParentID"] = PID;
DR["tmp1"] = dtExcel.Rows[i][3].ToString();
DR["tmp2"] = float.Parse(dtExcel.Rows[i][4].ToString());
DR["tmp3"] = dtExcel.Rows[i][5].ToString();
}
DT.Rows.Add(DR);
}
else
{
list.AlertMsg("该行数据为空!");
}
}
DataRow[] Last = DT.Select("tmp2>0");
LastDT = Last[0].Table;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
using (System.Data.SqlClient.SqlBulkCopy bulkCopy =
new System.Data.SqlClient.SqlBulkCopy(@"Data Source=PC-200906271415\WS_HGO;Initial Catalog=F:\我的单子\200904\项目\ZYT\APP_DATA\BJYZT.MDF;User ID=sa;Password=123456;max pool size=512;pooling=true;"))
{
bulkCopy.DestinationTableName =
"BJProduct";
bulkCopy.ColumnMappings.Add("Title", "Title");
bulkCopy.ColumnMappings.Add("ParentID", "ParentID");
bulkCopy.ColumnMappings.Add("tmp1", "tmp1");
bulkCopy.ColumnMappings.Add("tmp2", "tmp2");
bulkCopy.ColumnMappings.Add("tmp3", "tmp3");
try
{
bulkCopy.WriteToServer(LastDT);
list.AlertMsg("数据导入成功!");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
DT.Clear();
}
}
}
//将Excel中的内容插入到DataTable中
public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties=Excel 8.0;";
string strExcel = string.Format("select * from [{0}$]", strSheetName);
DataSet ds = new DataSet();
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn))
{
conn.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds);
conn.Close();
conn.Dispose();
}
return ds.Tables[0];
}
//获取父类ID
public static int CategoryID(string category)
{
int CID = 1;
string s = "cn";
System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection();
Conn.ConnectionString = @"Data Source=PC-200906271415\WS_HGO;Initial Catalog=F:\我的单子\200904\项目\ZYT\APP_DATA\BJYZT.MDF;User ID=sa;Password=123456;";
Conn.Open();
string sqlcategory = "insert into BJProductCategory (Title,ParentID,Version) values('" + category + "'," + 1 + ",'" + s + "')";
System.Data.SqlClient.SqlCommand Com = new System.Data.SqlClient.SqlCommand(sqlcategory, Conn);
int i = Com.ExecuteNonQuery();
if (i > 0)
{
//取出最大值就是当前插入的ID
string str = "select max(ID) AS ID from BJProductCategory";
System.Data.SqlClient.SqlCommand Cmd = new System.Data.SqlClient.SqlCommand(str, Conn);
CID = Convert.ToInt32(Cmd.ExecuteScalar());
}
Conn.Close();
Conn.Dispose();
return CID;
}
//Title,ParentID,tmp1,tmp2,tmp3,Version
public static DataTable CreateParentTable()
{
ParentTable.Columns.Clear();
DC = new DataColumn();
DC.ColumnName = "Title";
DC.DataType = System.Type.GetType("System.String");
ParentTable.Columns.Add(DC);
DC = new DataColumn();
DC.ColumnName = "ParentID";
DC.DataType = System.Type.GetType("System.Int32");
ParentTable.Columns.Add(DC);
DC = new DataColumn();
DC.ColumnName = "tmp1";
DC.DataType = System.Type.GetType("System.String");
ParentTable.Columns.Add(DC);
DC = new DataColumn();
DC.ColumnName = "tmp2";
DC.DataType = System.Type.GetType("System.String");
ParentTable.Columns.Add(DC);
DC = new DataColumn();
DC.ColumnName = "tmp3";
DC.DataType = System.Type.GetType("System.String");
ParentTable.Columns.Add(DC);
DC = new DataColumn();
DC.ColumnName = "Version";
DC.DataType = System.Type.GetType("System.String");
ParentTable.Columns.Add(DC);
return ParentTable;
}
Excel =(path + System.IO.Path.GetFileName(FileUpload1.FileName));
FileUpload1.PostedFile.SaveAs(Excel);