asp.net的问题,怎样导入word文件中的试题数据到数据库?
现在我已经在后台用C#代码实现打开word文档,得到所有的数据,但不知道怎么取到相应数据,类型,题目,选项答案,正确答案.(还有就是老是打开Word文档,弹出word文档...
现在我已经在后台用C#代码实现打开word文档,得到所有的数据,但不知道怎么取到相应数据,类型,题目,选项答案,正确答案.
(还有就是老是打开Word文档,弹出word文档,然后需要关闭word文档才能继续取题) 展开
(还有就是老是打开Word文档,弹出word文档,然后需要关闭word文档才能继续取题) 展开
3个回答
2013-07-29
展开全部
前台:
<div style="text-align:center;">
上传试题所属类型:
<asp:DropDownList ID="ddlques_type" runat="server" Height="23px" Width="148px">
</asp:DropDownList><br/>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnShangChuan" runat="server" Text="开始上传Word数据"
onclick="btnShangChuan_Click" Width="131px"/>
<asp:Label ID="lblText" runat="server" Text="*" ForeColor="#FF3300"></asp:Label>
注意:上传文件后缀名为.doc或.docx的Word文件
</div>
后台:
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
if (this.FileUpload1.FileName.Trim() == "")
{
this.lblText.Text = "请选择要上传Word文件!";
return;
}
//筛选合法文件
string fileName = this.FileUpload1.FileName;
string fileType = ".doc,.docx";
int dian = fileName.LastIndexOf('.');
string exten = fileName.Substring(dian);//得到后缀名
int IsHas = fileType.IndexOf(exten);//根据后缀名查找对比
if (IsHas == -1)//没有找到对应的后缀名称时
{
this.lblText.Text = "不允许上传非法图片!";
this.FileUpload1.Focus();
return;
}
else
{
object oMissing = System.Reflection.Missing.Value;
//打开文档
Microsoft.Office.Interop.Word._Application oWord = null;
Microsoft.Office.Interop.Word._Document oDoc = null;//记录word打开的文档
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
//得到绝对的路径
//string path = this.FileUpload1.PostedFile.FileName;//IE问题,现只有IE6能得到全路径
object path = @"E:\" + FileUpload1.FileName; //暂时取的死路径
//打开文档
oDoc = oWord.Documents.Open(ref path,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
object unknow = Type.Missing;
object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
OnlineExam exam = null;
List<OnlineExam> list = new List<OnlineExam>();
for (int i = 1; i < oDoc.Paragraphs.Count; i++)
{
if (i == 1 || i % 6 == 1)//题目
{
exam = new OnlineExam();
exam.Content = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim());
}
if (i == 2 || i % 6 == 2)//答案A
{
exam.Ques_key_a = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim().Replace("A.",""));
}
if (i == 3 || i % 6 == 3)//答案B
{
exam.Ques_key_b = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim().Replace("B.", ""));
}
if (i == 4 || i % 6 == 4)//答案C
{
exam.Ques_key_c = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim().Replace("C.", ""));
}
if (i == 5 || i % 6 == 5)//答案D
{
exam.Ques_key_d = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim().Replace("D.", ""));
}
if (i == 6 || i % 6 == 0)//正确答案
{
string correctKey = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim());
int indexof = correctKey.IndexOf(":");
if (indexof < 0)
{
//关闭word文档
oWord.ActiveDocument.Close(ref saveChanges, ref unknow, ref unknow);
//关闭word程序
oWord.Quit(ref saveChanges, ref unknow, ref unknow);
this.lblText.Text = "第" + i/6 + "题附近,试题文档标准格式有误,请检查!";
return;
}
string correctKey2 = correctKey.Substring(indexof + 1, correctKey.Length - (indexof + 1));
exam.Ques_correctKey = correctKey2;
if (correctKey2.Split(',').Length > 1)
{
exam.Ques_check_type = 1;//多选
}
else
{
exam.Ques_check_type = 0;//单选
}
list.Add(exam);
}
}
if (list.Count > 0)
{
questions_info questions = null;
for (int i = 0; i < list.Count; i++)
{
questions = new questions_info();
questions.ques_typeId = Convert.ToInt32(ddlques_type.SelectedValue);
questions.ques_check_type = Convert.ToInt32(list[i].Ques_check_type);
questions.ques_registTime = DateTime.Now;
questions.content = list[i].Content;
questions.ques_key_a = list[i].Ques_key_a;
questions.ques_key_b = list[i].Ques_key_b;
questions.ques_key_c = list[i].Ques_key_c;
questions.ques_key_d = list[i].Ques_key_d;
questions.ques_correctKey = list[i].Ques_correctKey;
examManageBll.InsertIntoQuestions(questions);
}
this.lblText.Text = "上传成功!";
}
if (list.Count == 0)
{
this.lblText.Text = "文档格式标准有误,请检查!";
}
//关闭word文档
oWord.ActiveDocument.Close(ref saveChanges, ref unknow, ref unknow);
//关闭word程序
oWord.Quit(ref saveChanges, ref unknow, ref unknow);
}
//文档是一行一行读的,不清楚还有没有更好的方法
<div style="text-align:center;">
上传试题所属类型:
<asp:DropDownList ID="ddlques_type" runat="server" Height="23px" Width="148px">
</asp:DropDownList><br/>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnShangChuan" runat="server" Text="开始上传Word数据"
onclick="btnShangChuan_Click" Width="131px"/>
<asp:Label ID="lblText" runat="server" Text="*" ForeColor="#FF3300"></asp:Label>
注意:上传文件后缀名为.doc或.docx的Word文件
</div>
后台:
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
if (this.FileUpload1.FileName.Trim() == "")
{
this.lblText.Text = "请选择要上传Word文件!";
return;
}
//筛选合法文件
string fileName = this.FileUpload1.FileName;
string fileType = ".doc,.docx";
int dian = fileName.LastIndexOf('.');
string exten = fileName.Substring(dian);//得到后缀名
int IsHas = fileType.IndexOf(exten);//根据后缀名查找对比
if (IsHas == -1)//没有找到对应的后缀名称时
{
this.lblText.Text = "不允许上传非法图片!";
this.FileUpload1.Focus();
return;
}
else
{
object oMissing = System.Reflection.Missing.Value;
//打开文档
Microsoft.Office.Interop.Word._Application oWord = null;
Microsoft.Office.Interop.Word._Document oDoc = null;//记录word打开的文档
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
//得到绝对的路径
//string path = this.FileUpload1.PostedFile.FileName;//IE问题,现只有IE6能得到全路径
object path = @"E:\" + FileUpload1.FileName; //暂时取的死路径
//打开文档
oDoc = oWord.Documents.Open(ref path,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
object unknow = Type.Missing;
object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
OnlineExam exam = null;
List<OnlineExam> list = new List<OnlineExam>();
for (int i = 1; i < oDoc.Paragraphs.Count; i++)
{
if (i == 1 || i % 6 == 1)//题目
{
exam = new OnlineExam();
exam.Content = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim());
}
if (i == 2 || i % 6 == 2)//答案A
{
exam.Ques_key_a = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim().Replace("A.",""));
}
if (i == 3 || i % 6 == 3)//答案B
{
exam.Ques_key_b = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim().Replace("B.", ""));
}
if (i == 4 || i % 6 == 4)//答案C
{
exam.Ques_key_c = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim().Replace("C.", ""));
}
if (i == 5 || i % 6 == 5)//答案D
{
exam.Ques_key_d = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim().Replace("D.", ""));
}
if (i == 6 || i % 6 == 0)//正确答案
{
string correctKey = Convert.ToString(oDoc.Paragraphs[i].Range.Text.Trim());
int indexof = correctKey.IndexOf(":");
if (indexof < 0)
{
//关闭word文档
oWord.ActiveDocument.Close(ref saveChanges, ref unknow, ref unknow);
//关闭word程序
oWord.Quit(ref saveChanges, ref unknow, ref unknow);
this.lblText.Text = "第" + i/6 + "题附近,试题文档标准格式有误,请检查!";
return;
}
string correctKey2 = correctKey.Substring(indexof + 1, correctKey.Length - (indexof + 1));
exam.Ques_correctKey = correctKey2;
if (correctKey2.Split(',').Length > 1)
{
exam.Ques_check_type = 1;//多选
}
else
{
exam.Ques_check_type = 0;//单选
}
list.Add(exam);
}
}
if (list.Count > 0)
{
questions_info questions = null;
for (int i = 0; i < list.Count; i++)
{
questions = new questions_info();
questions.ques_typeId = Convert.ToInt32(ddlques_type.SelectedValue);
questions.ques_check_type = Convert.ToInt32(list[i].Ques_check_type);
questions.ques_registTime = DateTime.Now;
questions.content = list[i].Content;
questions.ques_key_a = list[i].Ques_key_a;
questions.ques_key_b = list[i].Ques_key_b;
questions.ques_key_c = list[i].Ques_key_c;
questions.ques_key_d = list[i].Ques_key_d;
questions.ques_correctKey = list[i].Ques_correctKey;
examManageBll.InsertIntoQuestions(questions);
}
this.lblText.Text = "上传成功!";
}
if (list.Count == 0)
{
this.lblText.Text = "文档格式标准有误,请检查!";
}
//关闭word文档
oWord.ActiveDocument.Close(ref saveChanges, ref unknow, ref unknow);
//关闭word程序
oWord.Quit(ref saveChanges, ref unknow, ref unknow);
}
//文档是一行一行读的,不清楚还有没有更好的方法
2013-07-29
展开全部
过来看下 原来都是高手呀 哈哈 闪`````````````
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-29
展开全部
很想帮助你,但是你能说明白点吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询