Exemple #1
0
        /// <summary>
        /// 批量重命名文件(当文件为Word时,修改标题中的文字)
        /// </summary>
        /// <param name="lbox">显示要重命名文件的listBox控件对象</param>
        /// <param name="intFlag">标识按编号重命名,还是按扩展名重命名</param>
        /// <param name="strExten">要重命名的扩展名</param>
        /// <param name="strOldName">要替换的标题文字</param>
        /// <param name="strNewName">替换后的标题文字</param>
        /// <param name="PBar">进度条</param>
        private void RepeatFile(ListBox lbox, int intFlag, string strExten, string strOldName, string strNewName, ProgressBar PBar)
        {
            FileInfo FInfo = null;

            PBar.Maximum = lbox.Items.Count;//设置进度条最大值
            for (int i = 0; i < lbox.Items.Count; i++)
            {
                string strFile = lbox.Items[i].ToString();//获取要重命名的文件列表
                if (File.Exists(strFile))
                {
                    FInfo = new FileInfo(strFile);
                    string strPath      = FInfo.DirectoryName; //获取文件路径
                    string strFName     = FInfo.Name;          //获取文件名
                    string strExtension = FInfo.Extension;     //获取文件扩展名
                    switch (intFlag)
                    {
                    case 0:    //在文件名前加编号
                        File.Move(strFile, strPath + "\\" + i.ToString().PadLeft(4, '0') + strFName);
                        break;

                    case 1:    //在文件名后加编号
                        File.Move(strFile, strPath + "\\" + strFName.Substring(0, strFName.LastIndexOf(".")) + i.ToString().PadLeft(4, '0') + strExtension);
                        break;

                    case 2:    //批量修改文件扩展名
                        File.Move(strFile, strPath + "\\" + strFName.Substring(0, strFName.LastIndexOf(".")) + "." + strExten);
                        break;

                    case 3:    //如果修改的文件为Word文件
                        frmRepeat frmrepeat  = new frmRepeat();
                        object    strNewPath = strPath + "\\" + strFName.Replace(strOldName, strNewName);
                        File.Move(strFile, strNewPath.ToString());
                        if (strFName.ToLower() == ".doc")
                        {
                            //若文件为Word文档格式则替换标题中的文字
                            Microsoft.Office.Interop.Word.Application newWord = new Microsoft.Office.Interop.Word.Application();
                            object missing = System.Reflection.Missing.Value;
                            //打开一个Word文档
                            Microsoft.Office.Interop.Word.Document newDocument = newWord.Documents.Open(ref strNewPath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                            object Replaceone = Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne;
                            //设置要查找的关键字
                            newWord.Selection.Find.ClearFormatting();
                            newWord.Selection.Find.Text = strOldName;
                            //设置要替换的关键字
                            newWord.Selection.Find.Replacement.ClearFormatting();
                            newWord.Selection.Find.Replacement.Text = strNewName;
                            newWord.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref Replaceone, ref missing, ref missing, ref missing, ref missing);
                            //保存Word文档
                            newWord.ActiveDocument.Save();
                            //关闭Word文档
                            object objSaveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges;
                            Microsoft.Office.Interop.Word.DocumentClass doc = newWord.ActiveDocument as Microsoft.Office.Interop.Word.DocumentClass;
                            doc.Close(ref objSaveChange, ref missing, ref missing);
                        }
                        break;
                    }
                    PBar.Value = i + 1;
                }
            }
        }
    public void ExportToWord(DataSet ds)
    {


        
        if (ds.Tables.Count>0)
        {
            string tempFileName = "临时的";
            object filename = null;

            object tableBehavior = Microsoft.Office.Interop.Word.WdDefaultTableBehavior.wdWord9TableBehavior;
            object autoFitBehavior = Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitFixed;

            object unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
            object extend = System.Reflection.Missing.Value;
            object breakType = (int)Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;

            object count = 1;
            object character = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;

            object Nothing = System.Reflection.Missing.Value;

            try
            {
                //tempFileName = GetTempFileName();

                //生成.doc文件完整路径名 
                filename ="112.doc";

                //创建一个word文件,文件名用系统时间生成精确到毫秒 
                Microsoft.Office.Interop.Word.Application myWord = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document myDoc = new Microsoft.Office.Interop.Word.DocumentClass();
                myDoc = myWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                myDoc.Activate();

                //向把dataset中的表插入到word的文件中 

                for (int totalTable = 0; totalTable < ds.Tables.Count; totalTable++)
                {
                    myWord.Application.Selection.TypeText(ds.Tables[totalTable].TableName + "表的数据如下");
                    myWord.Application.Selection.TypeParagraph();
                    myWord.Application.Selection.TypeParagraph();
                    Microsoft.Office.Interop.Word.Range para = myWord.Application.Selection.Range;
                    myDoc.Tables.Add(para, ds.Tables[totalTable].Rows.Count + 1, ds.Tables[totalTable].Columns.Count, ref tableBehavior, ref autoFitBehavior);
                    for (int column = 0; column < ds.Tables[totalTable].Columns.Count; column++)
                    {
                        myDoc.Tables[0].Cell(1, column + 1).Range.InsertBefore(ds.Tables[0].Columns[column].ColumnName.Trim());

                    }
                    for (int row = 0; row < ds.Tables[totalTable].Rows.Count; row++)
                    {
                        for (int column = 0; column < ds.Tables[totalTable].Columns.Count; column++)
                        {
                            myDoc.Tables[1].Cell(row + 2, column + 1).Range.InsertBefore(ds.Tables[totalTable].Rows[row][column].ToString().Trim());
                        }
                    }
                    myWord.Application.Selection.EndKey(ref unit, ref extend);
                    myWord.Application.Selection.TypeParagraph();
                    myWord.Application.Selection.TypeParagraph();
                    myWord.Application.Selection.InsertBreak(ref breakType);
                }
                myWord.Application.Selection.TypeBackspace();
                myWord.Application.Selection.Delete(ref character, ref count);
                myWord.Application.Selection.HomeKey(ref unit, ref extend);
                //保存word文件到指定的目录下 
                try
                {
                    myDoc.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);
                    myWord.Visible = true;
                    object missingValue = Type.Missing;
                    object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                    myDoc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
                    object miss = System.Reflection.Missing.Value;
                    myDoc.Application.Quit(ref miss, ref miss, ref miss);
                    // doc = null;
                    myWord = null;

                }
                catch
                {
                    // System.Windows.Forms.MessageBox.Show("系统找不到指定目录下的文件: " + DATAWORDPATH + tempFileName + WORDPOSTFIX);
                    HttpContext.Current.Response.Write("<script>alert('系统找不到指定目录下的文件:" + DATAWORDPATH + tempFileName + WORDPOSTFIX + "')<script>");
                    return;
                }
               

                //让生成的word文件可见 
              //  myWord.Visible = true;
            }
            catch (Exception ex)
            {
                //   System.Windows.Forms.MessageBox.Show("向word文件中写入数据出错: " + ex.Message);
                HttpContext.Current.Response.Write("<script>alert('向word文件中写入数据出错:" + ex.Message + "')<script>");
            }
        }
        else
        {
            //   System.Windows.Forms.MessageBox.Show("No Data");
            HttpContext.Current.Response.Write("<script>alert('没有数据。')<script>");

        }
    }
    public void ExportToWord(DataSet ds)
    {
        if (ds.Tables.Count > 0)
        {
            string tempFileName = "临时的";
            object filename     = null;

            object tableBehavior   = Microsoft.Office.Interop.Word.WdDefaultTableBehavior.wdWord9TableBehavior;
            object autoFitBehavior = Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitFixed;

            object unit      = Microsoft.Office.Interop.Word.WdUnits.wdStory;
            object extend    = System.Reflection.Missing.Value;
            object breakType = (int)Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;

            object count     = 1;
            object character = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;

            object Nothing = System.Reflection.Missing.Value;

            try
            {
                //tempFileName = GetTempFileName();

                //生成.doc文件完整路径名
                filename = "112.doc";

                //创建一个word文件,文件名用系统时间生成精确到毫秒
                Microsoft.Office.Interop.Word.Application myWord = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document    myDoc  = new Microsoft.Office.Interop.Word.DocumentClass();
                myDoc = myWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                myDoc.Activate();

                //向把dataset中的表插入到word的文件中

                for (int totalTable = 0; totalTable < ds.Tables.Count; totalTable++)
                {
                    myWord.Application.Selection.TypeText(ds.Tables[totalTable].TableName + "表的数据如下");
                    myWord.Application.Selection.TypeParagraph();
                    myWord.Application.Selection.TypeParagraph();
                    Microsoft.Office.Interop.Word.Range para = myWord.Application.Selection.Range;
                    myDoc.Tables.Add(para, ds.Tables[totalTable].Rows.Count + 1, ds.Tables[totalTable].Columns.Count, ref tableBehavior, ref autoFitBehavior);
                    for (int column = 0; column < ds.Tables[totalTable].Columns.Count; column++)
                    {
                        myDoc.Tables[0].Cell(1, column + 1).Range.InsertBefore(ds.Tables[0].Columns[column].ColumnName.Trim());
                    }
                    for (int row = 0; row < ds.Tables[totalTable].Rows.Count; row++)
                    {
                        for (int column = 0; column < ds.Tables[totalTable].Columns.Count; column++)
                        {
                            myDoc.Tables[1].Cell(row + 2, column + 1).Range.InsertBefore(ds.Tables[totalTable].Rows[row][column].ToString().Trim());
                        }
                    }
                    myWord.Application.Selection.EndKey(ref unit, ref extend);
                    myWord.Application.Selection.TypeParagraph();
                    myWord.Application.Selection.TypeParagraph();
                    myWord.Application.Selection.InsertBreak(ref breakType);
                }
                myWord.Application.Selection.TypeBackspace();
                myWord.Application.Selection.Delete(ref character, ref count);
                myWord.Application.Selection.HomeKey(ref unit, ref extend);
                //保存word文件到指定的目录下
                try
                {
                    myDoc.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);
                    myWord.Visible = true;
                    object missingValue     = Type.Missing;
                    object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                    myDoc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
                    object miss = System.Reflection.Missing.Value;
                    myDoc.Application.Quit(ref miss, ref miss, ref miss);
                    // doc = null;
                    myWord = null;
                }
                catch
                {
                    // System.Windows.Forms.MessageBox.Show("系统找不到指定目录下的文件: " + DATAWORDPATH + tempFileName + WORDPOSTFIX);
                    HttpContext.Current.Response.Write("<script>alert('系统找不到指定目录下的文件:" + DATAWORDPATH + tempFileName + WORDPOSTFIX + "')<script>");
                    return;
                }


                //让生成的word文件可见
                //  myWord.Visible = true;
            }
            catch (Exception ex)
            {
                //   System.Windows.Forms.MessageBox.Show("向word文件中写入数据出错: " + ex.Message);
                HttpContext.Current.Response.Write("<script>alert('向word文件中写入数据出错:" + ex.Message + "')<script>");
            }
        }
        else
        {
            //   System.Windows.Forms.MessageBox.Show("No Data");
            HttpContext.Current.Response.Write("<script>alert('没有数据。')<script>");
        }
    }