两张表导入到一个DataGrid里面(题目表和答案表)
前台代码
<asp:Content ID="Content1" ContentPlaceHolderID="cphToolBar" runat="server">
<epoint:Button ID="btnImport" runat="server" Text="题库信息导入" />
<span>
<span style="color: red;">模板下载:</span><a target="_blank"
href="题库模版.xls"><span style="color: blue;">点击下载文件</span> </a>
</span>
</asp:Content> <asp:UpdatePanel runat="server" ID="UpdatePanel_Upload">
<ContentTemplate>
<epoint:CuteWebUIUpload_NoBut ID="upload1" AllowFileList="xlsx;xls" runat="server"
MaxAttachCount="-1" MaxAttachCountOneTime="" UseCustomSaveMethod="true" OnFileUploadCompleted_Custom="upload1_FileUploadCompleted_Custom" />
</ContentTemplate>
</asp:UpdatePanel>
后台代码
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param> protected void upload1_FileUploadCompleted_Custom(object sender, EventArgsOperate.AttachEventArgs[] args)
{
if (!Directory.Exists(Server.MapPath(@"ImportExcel")))
Directory.CreateDirectory(Server.MapPath(@"ImportExcel"));
string mark = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
string oldfileName = args[].FileName;
string documentType = oldfileName.Substring(oldfileName.LastIndexOf('.'), oldfileName.Length - oldfileName.LastIndexOf('.'));
string fileName = "Import_" + mark + documentType;
args[].CuteArgs.CopyTo(Server.MapPath(@"ImportExcel\") + fileName); ReadExcel(Server.MapPath(@"ImportExcel\") + fileName);
} public void ReadExcel(string ExcelFile)
{
DataSet ds;
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFile + ";" + "Extended Properties='Excel 12.0';";
OleDbConnection conn = new OleDbConnection(strConn);
DataTable dtExcelSchema = new DataTable();
try
{
conn.Open();
dtExcelSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] { null, null, null, "Table" });//获取需要上传的Excel的Sheet
conn.Close();
}
catch
{
throw;
}
for (int k = ; k <= dtExcelSchema.Rows.Count; k++)
{
try
{
ds = new DataSet();
OleDbDataAdapter oada = new OleDbDataAdapter("select * from [Sheet" + k + "$]", strConn);
oada.Fill(ds);
}
catch
{
throw;
} DataTable dt = ds.Tables[];
if (dt.Rows.Count > )
{ for (int i = ; i < dt.Rows.Count; i++)
{
M_Exam_Subject m_es = new M_Exam_Subject();
M_Exam_Answer m_ea = new M_Exam_Answer();
#region 插入题目
m_es.SubjectGuid = NewSubjectGuid();
m_es.Title = Convert.ToString(dt.Rows[i][]);//题目名称
string type = Convert.ToString(dt.Rows[i][]);//题目类型
switch (type)
{
case "单选": m_es.Type = "";
break;
case "多选": m_es.Type = "";
break;
case "判断": m_es.Type = "";
break;
case "填空": m_es.Type = "";
break;
case "简答": m_es.Type = "";
break;
} string difficult = Convert.ToString(dt.Rows[i][]);//题目难度
switch (difficult)
{
case "简单": m_es.Difficult = ;
break;
case "一般": m_es.Difficult = ;
break;
case "难": m_es.Difficult = ;
break;
}
m_es.AnswerNote = Convert.ToString(dt.Rows[i][]);//答案解析
m_es.GroupID = Convert.ToInt32(ParentRowID);
b_examsubject.Insert(m_es);
#endregion
//插入主观题答案
if ((type == "填空") || (type == "简答"))
{
m_es.SubjectGuid = m_es.SubjectGuid;
m_es.RightAnswer = Convert.ToString(dt.Rows[i][]);//正确答案
b_examsubject.Update(m_es);
}
//插入客观题答案
else
{
//for (int j = 3; j < 7; j++)
//{
// m_ea.SubjectGuid = m_es.SubjectGuid;
// m_ea.AnswerGuid = Guid.NewGuid().ToString();
// m_ea.AnswerName = Convert.ToString(dt.Rows[i][j]);//答案
// b_examanswer.Insert(m_ea);
//}
DataView dvRecord = dt.DefaultView;
string answerid1 = Guid.NewGuid().ToString();
string answerid2 = Guid.NewGuid().ToString();
string answerid3 = Guid.NewGuid().ToString();
string answerid4 = Guid.NewGuid().ToString();
string answerid5 = Guid.NewGuid().ToString();
string answerid6 = Guid.NewGuid().ToString();
if (Convert.ToString(dvRecord[i][]) != "")
b_examanswer.InsertAnswer(answerid1, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
if (Convert.ToString(dvRecord[i][]) != "")
b_examanswer.InsertAnswer(answerid2, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
if (Convert.ToString(dvRecord[i][]) != "")
b_examanswer.InsertAnswer(answerid3, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
if (Convert.ToString(dvRecord[i][]) != "")
b_examanswer.InsertAnswer(answerid4, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
if (Convert.ToString(dvRecord[i][]) != "")
b_examanswer.InsertAnswer(answerid5, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid);
if (Convert.ToString(dvRecord[i][]) != "")
b_examanswer.InsertAnswer(answerid6, Convert.ToString(dvRecord[i][]), false, m_es.SubjectGuid); //添加正确答案
int num = ;
if (Convert.ToString(dvRecord[i][]) != "")
{
string strright = Convert.ToString(dvRecord[i][]).Trim();
if (strright.IndexOf('A') >= || strright.IndexOf('a') >= )
{
num++;
b_examanswer.UpdateAnswer_isRight(answerid1, true);
}
if (strright.IndexOf('B') >= || strright.IndexOf('b') >= )
{
num++;
b_examanswer.UpdateAnswer_isRight(answerid2, true);
}
if (strright.IndexOf('C') >= || strright.IndexOf('c') >= )
{
num++;
b_examanswer.UpdateAnswer_isRight(answerid3, true);
}
if (strright.IndexOf('D') >= || strright.IndexOf('d') >= )
{
num++;
b_examanswer.UpdateAnswer_isRight(answerid4, true);
}
if (strright.IndexOf('E') >= || strright.IndexOf('e') >= )
{
num++;
b_examanswer.UpdateAnswer_isRight(answerid5, true);
}
if (strright.IndexOf('F') >= || strright.IndexOf('f') >= )
{
num++;
b_examanswer.UpdateAnswer_isRight(answerid6, true);
}
}
}
}
}
}
BindGrid();
}
单表导入到一个DataGrid里面
后台代码
protected void InfoExport()
{
try
{
string ExcelName = this.CreateExcel();
//将服务器上的Excel导出
// CuteWebUIOperate.DownloadFile(HttpContext.Current, Server.MapPath("ExcelExport/") + ExcelName, ExcelName, false);
string strScript = "window.open('ExcelExport/" + ExcelName + "');";
this.WriteAjaxMessage(strScript);
}
catch
{
throw;
}
} protected string CreateExcel() //生成Excel
{
string Header = "报名信息";
string strFileName = ""; // 生成文件夹
string fileFolderPath = Server.MapPath("ExcelExport/");
if (!System.IO.Directory.Exists(fileFolderPath))
System.IO.Directory.CreateDirectory(fileFolderPath); Workbook wb = new Workbook(); wb.Worksheets.Add("Sheet1"); Worksheet ws = wb.ActiveWorksheet;
//first row 19cell
WorksheetMergedCellsRegionCollection wm = ws.MergedCellsRegions; WorksheetMergedCellsRegion wmc = wm.Add(, , , );//起始位置和终止位置
wmc.Value = Header;
wmc.CellFormat.Alignment = HorizontalCellAlignment.Center;
wmc.CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
wmc.CellFormat.BottomBorderColor = Color.Black;
wmc.CellFormat.LeftBorderColor = Color.Black;
wmc.CellFormat.RightBorderColor = Color.Black;
wmc.CellFormat.TopBorderColor = Color.Black; wmc.CellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
wmc.CellFormat.WrapText = ExcelDefaultableBoolean.True;
wmc.CellFormat.Font.Name = "宋体";
//字体大小
wmc.CellFormat.Font.Height = ;
IWorksheetCellFormat HeadCellFormat = wb.CreateNewWorksheetCellFormat();
HeadCellFormat.Alignment = HorizontalCellAlignment.Center;
HeadCellFormat.Font.Bold = ExcelDefaultableBoolean.True;
HeadCellFormat.Font.Name = "宋体"; HeadCellFormat.BottomBorderColor = Color.Black;
HeadCellFormat.LeftBorderColor = Color.Black;
HeadCellFormat.RightBorderColor = Color.Black;
HeadCellFormat.TopBorderColor = Color.Black; HeadCellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
HeadCellFormat.WrapText = ExcelDefaultableBoolean.True; IWorksheetCellFormat ItemCellFormat = wb.CreateNewWorksheetCellFormat();
//CellFormat.Alignment = HorizontalCellAlignment.Center;
//CellFormat.Font.Bold = ExcelDefaultableBoolean.True;
ItemCellFormat.FillPattern = FillPatternStyle.Default;
ItemCellFormat.ShrinkToFit = ExcelDefaultableBoolean.True;
ItemCellFormat.BottomBorderColor = Color.Black;
ItemCellFormat.LeftBorderColor = Color.Black;
ItemCellFormat.RightBorderColor = Color.Black;
ItemCellFormat.TopBorderColor = Color.Black; ItemCellFormat.BottomBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.LeftBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.RightBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.TopBorderStyle = CellBorderLineStyle.Thin;
ItemCellFormat.WrapText = ExcelDefaultableBoolean.True;
ItemCellFormat.FormatString = "##,##0.00";
ItemCellFormat.Font.Name = "宋体";
int n;
n = ;
wmc = wm.Add(, n, , n++);
wmc.Value = "序号";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "姓名";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "身份证号";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "单位名称";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "计划名称";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "报名项";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "准考证号";
wmc.CellFormat.SetFormatting(HeadCellFormat); wmc = wm.Add(, n, , n++);
wmc.Value = "成绩";
wmc.CellFormat.SetFormatting(HeadCellFormat); ws.Columns[n].Width = * ;
wmc = wm.Add(, n, , n++);
wmc.Value = "是否合格";
wmc.CellFormat.SetFormatting(HeadCellFormat); DataView dv = GetExcelData();//获取报名信息
for (int i = ; i < dv.Count; i++)
{
n = ; wmc = wm.Add(i + , n, i + , n++);
wmc.Value = Convert.ToString(i + );
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["Name"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["IdentityNum"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["DanWeiName"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["PlanName"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["ItemName"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["ZhunKZNum"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["Score"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); wmc = wm.Add(i + , n, i + , n++);
wmc.Value = dv[i]["IsPass"].ToString();
wmc.CellFormat.SetFormatting(ItemCellFormat); } string mark = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
strFileName = "Export_" + mark + ".xls";
BIFF8Writer.WriteWorkbookToFile(wb, Server.MapPath("ExcelExport/" + strFileName)); return strFileName;
} protected DataView GetExcelData()
{
int TotalNum = ;
string where = " where 1=1 ";
if (!String.IsNullOrEmpty(txtIdentityNum.Text))
{
where += "and IdentityNum like '%" + txtIdentityNum.Text + "%'";
}
where += "and PlanGuid = '" + PlanGuid + "'";
where += "and Status != '" + + "'";
string connectionStringName = "DJG_PeiXun_ConnectionString";
string fields = "*";
string sortExpression = "order by Row_ID desc"; DataTable DvPaging = new DB_Common().GetData_Page_Table(
fields,
DataGrid1.PageSize,
DataGrid1.CurrentPageIndex + ,
"View_Score_UserType",
"Row_ID",
where,
sortExpression,
out TotalNum,
connectionStringName
);
return DvPaging.DefaultView;
}