コード例 #1
0
 private void butCreatWord_Click(object sender, EventArgs e)
 {
     Loading.loading f = new Loading.loading();  //实例化进度条
     if (MessageBox.Show("确定要将所选实验生成Word文档?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)//判断是否在弹出的对话框中单击"确定"按钮
     {
         FolderBrowserDialog fbd = new FolderBrowserDialog();
         fbd.ShowDialog();
         string filesave = fbd.SelectedPath;//获得选择的文件夹路径来存储
         if (filesave != "")
         {
             DataClass.Report.Report_Path = filesave;//保存最近的实验报告生成路径
             WordHandle word = new WordHandle();
             int Celect_num = 0;
             string SaveFile = "c:\\a.jpg";
             string docname;//文档名字
             string reporthead;//文档头
             for (int i = 0; i < dataGridView1.Rows.Count; i++)  //循环遍历DataGridView控件中的每一行
             {
                 if (dataGridView1.Rows[i].Cells[0].Value != null) //当当前单元格的内容不为空时
                 {
                     if (bool.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()) == true) //单元格选中
                     {
                         Celect_num++;
                     }
                 }
             }
             if (Celect_num == 0)//如果没有勾选项
             {
                 MessageBox.Show("请选择操作项!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information); //显示消息
             }
             else
             {//如果选择了项目则开始生成word
                 f.LoadOpen(this);  //打开进度条
                 for (int i = 0; i < dataGridView1.Rows.Count; i++)  //循环遍历DataGridView控件中的每一行
                 {
                     try
                     {
                         if (dataGridView1.Rows[i].Cells[0].Value != null) //当当前单元格的内容不为空时
                         {
                             if (bool.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()) == true) //单元格选中
                             {
                                 Celect_num++;
                                 string ide = dataGridView1.Rows[i].Cells[1].Value.ToString();//实验总编号
                                 string idc = dataGridView1.Rows[i].Cells[4].Value.ToString();//章节编号
                                 string idt = dataGridView1.Rows[i].Cells[3].Value.ToString();//教师号
                                 SqlDataReader temDR2 = MyClass.getcom("select * from tb_Chapter where IDC='" + idc.ToString() + "'");
                                 SqlDataReader temDR3 = MyClass.getcom("select * from tb_Teacher where IDT='" + idt.ToString() + "'");
                                 if (temDR2.Read() && temDR3.Read())
                                 {
                                     docname = "\\" + ide.ToString() + "_" + dataGridView1.Rows[i].Cells[5].Value.ToString() + "_" + temDR3.GetString(temDR3.GetOrdinal("class")) + "_" + temDR3.GetString(temDR3.GetOrdinal("name")) + ".doc";//编辑头信息
                                     word.CreateAWord();
                                     word.InsertText(dataGridView1.Rows[i].Cells[5].Value.ToString(), 20, Word.WdColor.wdColorDarkRed, 2, Word.WdParagraphAlignment.wdAlignParagraphCenter);
                                     word.NewLine();
                                     reporthead = "实验编号:" + ide.ToString() + "    教师号:" + idt.ToString() + "    班级:" + temDR3.GetString(temDR3.GetOrdinal("class")) + "    教师姓名:" + temDR3.GetString(temDR3.GetOrdinal("name")) + "    时间:" + DateTime.Now.AddDays(0).ToString("yyyy年MM月dd日") + "    所属章节:" + temDR2.GetString(temDR2.GetOrdinal("name"));
                                     word.InsertText(reporthead, 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphCenter);
                                     word.NewLine();
                                     word.InsertText("一.实验题目", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                     word.NewLine();
                                     word.InsertText(dataGridView1.Rows[i].Cells[5].Value.ToString(), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                     word.NewLine();
                                     word.InsertText("二.实验目的", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                     word.NewLine();
                                     word.InsertText(dataGridView1.Rows[i].Cells[6].Value.ToString(), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                     word.NewLine();
                                     word.InsertText("三.实验内容", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                     word.NewLine();
                                     word.InsertText(dataGridView1.Rows[i].Cells[7].Value.ToString(), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                     word.NewLine();
                                     word.InsertText("四.准备知识", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                     word.NewLine();
                                     word.InsertText(dataGridView1.Rows[i].Cells[8].Value.ToString(), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                     word.NewLine();
                                     word.InsertText("五.实验指导", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                     word.NewLine();
                                     word.InsertText(dataGridView1.Rows[i].Cells[9].Value.ToString(), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                     word.NewLine();
                                     word.SaveWord(filesave + docname, docname);//保存路径
                                     File.Delete(SaveFile);
                                 }
                             }
                         }
                     }
                     catch (Exception ex)//捕获异常
                     {
                         //f.LoadClose(this); //关闭进度条
                         MessageBox.Show(ex.Message);//弹出异常提示信息
                         break;
                     }
                 }
                 f.LoadClose(this); //关闭进度条
                 MessageBox.Show("生成文档结束!", "操作", MessageBoxButtons.OK, MessageBoxIcon.Information); //显示消息
             }
         }
     }
 }
コード例 #2
0
        private void butCreatWord_Click(object sender, EventArgs e)
        {
            Loading.loading f = new Loading.loading();  //实例化进度条
            List<Image> pic = new List<Image>();//存放大图
            if (MessageBox.Show("确定要将所选报告生成Word文档?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)//判断是否在弹出的对话框中单击"确定"按钮
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                fbd.ShowDialog();
                string filesave = fbd.SelectedPath;//获得选择的文件夹路径来存储
                if (filesave != "")
                {
                    DataClass.Report.Report_Path = filesave;//保存最近的实验报告生成路径
                    WordHandle word = new WordHandle();
                    int Select_num = 0;//变量用于存储勾选变量
                    string SaveFile = "c:\\a.jpg";
                    string docname;//文档名字
                    string reporthead;//文档头
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)  //循环遍历DataGridView控件中的每一行
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value != null) //当当前单元格的内容不为空时
                        {
                            if (bool.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()) == true) //单元格选中
                            {
                                Select_num++;
                            }
                        }
                    }
                    if (Select_num == 0)//如果没有勾选项
                    {
                        MessageBox.Show("请选择操作项!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information); //显示消息
                    }
                    else
                    {//如果选择了项目则开始生成word
                        f.LoadOpen(this);  //打开进度条
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)  //循环遍历DataGridView控件中的每一行
                        {
                            try
                            {
                                if (dataGridView1.Rows[i].Cells[0].Value != null) //当当前单元格的内容不为空时
                                {
                                    if (bool.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()) == true) //单元格选中
                                    {
                                        string idr = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                        string ide = dataGridView1.Rows[i].Cells[2].Value.ToString();
                                        string ids = dataGridView1.Rows[i].Cells[3].Value.ToString();
                                        SqlDataReader temDR = MyClass.getcom("select * from tb_Report where IDR='" + idr.ToString() + "'");
                                        SqlDataReader temDR2 = MyClass.getcom("select * from tb_Experiment where ID='" + ide.ToString() + "'");
                                        SqlDataReader temDR3 = MyClass.getcom("select * from tb_Student where IDS='" + ids.ToString() + "'");
                                        if (temDR.Read() && temDR2.Read() && temDR3.Read())
                                        {

                                            docname = "\\" + ide.ToString() + "_" + ids.ToString() + "_" + temDR3.GetString(temDR3.GetOrdinal("class")) + "_" + temDR3.GetString(temDR3.GetOrdinal("name")) + ".doc";//编辑头信息
                                            word.CreateAWord();
                                            word.InsertText(temDR2.GetString(temDR2.GetOrdinal("title")), 20, Word.WdColor.wdColorDarkRed, 2, Word.WdParagraphAlignment.wdAlignParagraphCenter);
                                            word.NewLine();
                                            reporthead = "实验编号:" + ide.ToString() + "    学号:" + ids.ToString() + "    班级:" + temDR3.GetString(temDR3.GetOrdinal("class")) + "    姓名:" + temDR3.GetString(temDR3.GetOrdinal("name")) + "    时间:" + temDR.GetString(temDR.GetOrdinal("date")) + "    分数:" + temDR.GetString(temDR.GetOrdinal("mark"));
                                            word.InsertText(reporthead, 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphCenter);
                                            word.NewLine();
                                            word.InsertText("一.实验目的", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText(temDR2.GetString(temDR2.GetOrdinal("purpose")), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText("二.问题描述", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText(temDR.GetString(temDR.GetOrdinal("description")), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText("三.算法设计", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText(temDR.GetString(temDR.GetOrdinal("ALG")), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText("四.运行结果", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText(temDR.GetString(temDR.GetOrdinal("running")), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText("附图:", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            //插图片
                                            pic = MyMC.reImage(ide.ToString(), ids);
                                            foreach (Image p in pic)
                                            {
                                                //p.Save(SaveFile, System.Drawing.Imaging.ImageFormat.Jpeg);//先存至路径
                                                //word.InsertPicture(SaveFile);
                                                //源代码会出现GDI+一般性错误因为用到的数据会处于锁定状态
                                                #region 解决方法
                                                /* 症状
                                         Bitmap 对象或一个 图像 对象从一个文件, 构造时该文件仍保留锁定对于对象的生存期。
                                         因此, 无法更改图像并将其保存回它产生相同的文件。
                                         替代方法
                                         创建非索引映像。
                                         创建索引映像。
                                         这两种情况下, 原始 位图 上调用 Bitmap.Dispose() 方法删除该文件上锁或删除要求, 流或内存保持活动。

                                         创建非索引图像
                                         即使原始映像被索引格式中该方法要求新图像位于每像素 (超过 8 位 -) -, 非索引像素格式。
                                         此变通方法使用 Graphics.DrawImage() 方法来将映像复制到新 位图 对象:
                                         1.    构造从流、 从内存, 或从文件原始 位图 。
                                         2.    创建新 位图 的相同大小, 带有是超过 8 位 - - 像素 (BPP) 每像素格式。
                                         3.    使用 Graphics.FromImage() 方法以获取有关二 位图 Graphics 对象。
                                         4.    用于 Graphics.DrawImage() 绘制首 位图 到二 位图 。
                                         5.    用于 Graphics.Dispose() 处置是 图形 。
                                         6.    用于 Bitmap.Dispose() 是首 位图 处置。

                                         创建索引映像
                                         此解决办法在索引格式创建一个 Bitmap 对象:
                                         1.    构造从流、 从内存, 或从文件原始 位图 。
                                         2.    创建新 位图 具有相同的大小和像素格式作为首 位图 。
                                         3.    使用 Bitmap.LockBits() 方法来锁定整个图像对于两 Bitmap 对象以其本机像素格式。
                                         4.    使用 Marshal.Copy 函数或其他内存复制函数来从首 位图 复制到二 位图 图像位。
                                         5.    使用 Bitmap.UnlockBits() 方法可以解锁两 Bitmap 对象。
                                         6.    用于 Bitmap.Dispose() 是首 位图 处置。
                                         */
                                                #endregion 解决方法
                                                Bitmap bmp = new Bitmap(p);
                                                //新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。
                                                Bitmap bmp2 = new Bitmap(p.Width, p.Height,
                                                System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
                                                //将第一个bmp拷贝到bmp2中
                                                Graphics draw = Graphics.FromImage(bmp2);
                                                draw.DrawImage(bmp, 0, 0);
                                                bmp2.Save(SaveFile, System.Drawing.Imaging.ImageFormat.Jpeg);
                                                draw.Dispose();
                                                bmp.Dispose();//释放bmp文件资源
                                                word.InsertPicture(SaveFile);
                                            }
                                            word.NewLine();
                                            pic.Clear();
                                            word.InsertText("五.问题与收获", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText(temDR.GetString(temDR.GetOrdinal("question")), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText("六.教师评价", 15, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.NewLine();
                                            word.InsertText(temDR.GetString(temDR.GetOrdinal("evaluation")), 10, Word.WdColor.wdColorBlack, 1, Word.WdParagraphAlignment.wdAlignParagraphLeft);
                                            word.SaveWord(filesave + docname, docname);//保存路径
                                            File.Delete(SaveFile);
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)//捕获异常
                            {
                                MessageBox.Show(ex.Message);//弹出异常提示信息
                                break;
                            }
                        }
                        f.LoadClose(this); //关闭进度条
                        MessageBox.Show("生成文档结束!", "操作", MessageBoxButtons.OK, MessageBoxIcon.Information); //显示消息
                    }
                }
            }
        }