Esempio n. 1
0
        /// <summary>
        /// 将Excel转成txt
        /// </summary>
        /// <param name="fileName"></param>
        public void Convert(string fileName)
        {
            if (!File.Exists(fileName))
            {
                //打印日志
                //Console.WriteLine("文件不存在");
                DMCommonMethod.WriteLog("文档管理", fileName + "文件不存在");
                return;
            }
            FileInfo info = new FileInfo(fileName);

            if (info.Extension != ".xls" && info.Extension != ".xlsx")
            {
                //打印日志
                //Console.WriteLine("文件格式错误");
                DMCommonMethod.WriteLog("文档管理", fileName + "文件格式错误");
                return;
            }
            destFileName = Path.Combine(DMCommonMethod.GetDMRootTempPath(), string.Format("_{0}.txt", CreateOnlyFileNameUtil.CreateOnlyFileName()));

            try
            {
                FileStream   fs = new FileStream(destFileName, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);

                var conn = new OleDbConnection();
                conn.ConnectionString = String.Format(@"provider=Microsoft.ACE.OLEDB.12.0;extended properties='excel 12.0 Macro;hdr=yes';data source={0}", fileName);
                conn.Open();
                DataTable sheetTb = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                foreach (DataRow sheet in sheetTb.Rows)
                {
                    string tableName = sheet["TABLE_NAME"].ToString();

                    string           sql = String.Format("select * from [{0}]", tableName);
                    OleDbDataAdapter da  = new OleDbDataAdapter(sql, conn);

                    var ds = new DataSet();
                    da.Fill(ds);

                    var tb1 = ds.Tables[0];

                    if (tb1.Rows.Count == 0)
                    {
                        continue; // 空表
                    }
                    if (tb1.Rows.Count == 1 && tb1.Columns.Count == 1)
                    {
                        if (tb1.Rows[0][0] == DBNull.Value)
                        {
                            continue; // 空表
                        }
                    }

                    int[] colMaxLen = new int[tb1.Columns.Count];

                    foreach (DataRow row in tb1.Rows)
                    {
                        for (int j = 0; j < tb1.Columns.Count; ++j)
                        {
                            DataColumn col     = tb1.Columns[j];
                            string     content = row[j].ToString();

                            bool hasYinhao = false;
                            if (-1 != content.IndexOf("\r") || -1 != content.IndexOf("\n"))
                            {
                                hasYinhao = true;
                            }
                            string fmt;
                            fmt = String.Format("{0}{1}0{2}{3}{4}", hasYinhao ? "\"" : "",
                                                "{", "}", hasYinhao ? "\"" : "", j + 1 == tb1.Columns.Count ? "" : "\t");
                            sw.Write(fmt, row[j]);
                        }
                        sw.WriteLine();
                    }
                }
                sw.Close();
                conn.Close();
            }
            catch (Exception ex)
            {
                DMCommonMethod.WriteLog("文档管理", fileName + "文件转换失败\n," + ex.Message);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 将PPT转成txt
        /// </summary>
        /// <param name="fileName"></param>
        public void Convert(string fileName)
        {
            if (!File.Exists(fileName))
            {
                //打印日志
                //Console.WriteLine("文件不存在");
                DMCommonMethod.WriteLog("文档管理", fileName + "文件不存在");
                return;
            }
            FileInfo info = new FileInfo(fileName);

            if (info.Extension != ".ppt" && info.Extension != ".pptx")
            {
                //打印日志
                //Console.WriteLine("文件格式错误");
                DMCommonMethod.WriteLog("文档管理", fileName + "文件格式错误");
                return;
            }
            destFileName = Path.Combine(DMCommonMethod.GetDMRootTempPath(), string.Format("_{0}.txt", CreateOnlyFileNameUtil.CreateOnlyFileName()));

            try
            {
                FileStream   fs = new FileStream(destFileName, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);

                Application  pa = new Application();
                Presentation pp = pa.Presentations.Open(fileName, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
                //开始读取每一页
                foreach (Slide slide in pp.Slides)
                {
                    //开始读取每一个数据块
                    foreach (Shape shape in slide.Shapes)
                    {
                        try
                        {
                            if (shape.TextFrame.TextRange != null)//如果是文字,文字处理
                            {
                                string text = shape.TextFrame.TextRange.Text.Trim();
                                sw.Write(text);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message == "指定的值超出了范围")
                            {
                                continue;
                            }
                        }
                    }
                    sw.WriteLine();
                }
                sw.Close();
            }
            catch (Exception ex)
            {
                DMCommonMethod.WriteLog("文档管理", fileName + "文件转换错误\n" + ex.Message);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 将word文档转成txt
        /// </summary>
        /// <param name="fileName">文档全路径</param>
        public void Convert(string fileName)
        {
            if (!File.Exists(fileName))
            {
                //打印日志
                Console.WriteLine("文件不存在");
                return;
            }
            FileInfo info = new FileInfo(fileName);

            if (info.Extension != ".doc" && info.Extension != ".docx")
            {
                //打印日志
                Console.WriteLine("文件格式错误");
                return;
            }
            destFileName = Path.Combine(DMCommonMethod.GetDMRootTempPath(), string.Format("_{0}.txt", CreateOnlyFileNameUtil.CreateOnlyFileName()));

            Application app = new Application();

            app.Visible = false;
            object obj       = System.Reflection.Missing.Value;
            object inputFile = fileName;

            Document doc = app.Documents.Open(ref inputFile, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj);

            object outfile = destFileName;
            object fmt     = WdSaveFormat.wdFormatEncodedText;

            doc.SaveAs(ref outfile, ref fmt, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj);
            doc.Close(ref obj, ref obj, ref obj);
            app.Quit();
            app = null;
        }