标签:style blog http color os io for ar 文件
导出试卷到WORD
#region 创建word试卷(直接导出为word) /// <summary> /// 创建word试卷 /// </summary> /// <param name="PathName"></param> public void CreateWordFile(string PathName) { try { Object Nothing = System.Reflection.Missing.Value; object filename = PathName; //文件保存路径 //1.创建Word文档 Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); //2.添加页眉 WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[入职考试试卷]"); WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置 WordApp.Selection.ParagraphFormat.LineSpacing = 8f;//设置文档的行间距 //3.移动焦点并换行 object count = 14; object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行; WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点 WordApp.Selection.TypeParagraph();//插入段落 #region 标题 WordApp.Selection.Font.Size = 20; WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; // 居中 WordApp.Selection.Font.Bold = 1; // 黑体 WordApp.Selection.TypeText("长城信息入职考试"); WordApp.Selection.TypeParagraph(); //WordApp.Selection.TypeParagraph(); #endregion //设置内容部分格式 WordApp.Selection.Font.Size = 12; WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; // 居中 WordApp.Selection.Font.Bold = 0; // 黑体 string sql = @"select * from base_tx"; DataTable dt = DataBaseAccess.GetDataTable(sql); for (int i = 0; i < dt.Rows.Count;i++ ) { //写标题 WordApp.Selection.TypeText((i+1)+"、"+dt.Rows[i]["name"].ToString()+"(每题3分)" + "\n\n"); int txid = Convert.ToInt32(dt.Rows[i]["id"]);//题型ID DataRow[] rows = dtxz.Select("xztxid=" + txid, "xztxid asc"); DataTable dttemp = dtxz.Clone(); dttemp.Clear(); foreach (DataRow dr2 in rows) { dttemp.Rows.Add(dr2.ItemArray); } for (int k = 0; k < dttemp.Rows.Count; k++) { string sqlstr = @"select * from base_st where id=" + dttemp.Rows[k]["xzstid"] + ""; DataTable dtst = DataBaseAccess.GetDataTable(sqlstr); //获取题目内容 byte[] bWrite = (byte[])dtst.Rows[0]["contents"];//从数据库中读出数据 string s = System.Text.Encoding.UTF8.GetString(bWrite, 0, bWrite.Length); //开始写内容 WordApp.Selection.TypeText(s); } } WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款” WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; //文件保存 WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); MessageBoxEx.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBoxEx.Show("导出失败!" + "\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } #endregion
附完整源代码下载:http://www.51aspx.com/Code/StarkSoftExam
StarkSoft题库管理系统(二)--生成word格式试卷
标签:style blog http color os io for ar 文件
原文地址:http://www.cnblogs.com/starksoft/p/3940361.html