1

我有一个应用程序,我需要在其中获取嵌入在 excel 中的对象应该通过代码存储在某个位置。

                        connection.Close();
                        //connection.ResetState();
                        string embeddingPartString;
                        //ArrayList chkdLstEmbeddedFiles = new ArrayList(); ;
                        List<String> chkdLstEmbeddedFiles = new List<string>();
                        //string fileName = txtSourceFile.Text;
                        if (filepath == string.Empty || !System.IO.File.Exists(filepath))
                        {
                            return;
                        }

                        // Open the package file 
                        pkg = Package.Open(filepath, FileMode.Open, FileAccess.ReadWrite);

                        System.IO.FileInfo fi = new System.IO.FileInfo(filepath);

                        string extension = fi.Extension.ToLower();

                        if ((extension == ".docx") || (extension == ".dotx") || (extension == ".docm") || (extension == ".dotm"))
                        {
                            embeddingPartString = "/word/embeddings/";
                        }
                        else if ((extension == ".xlsx") || (extension == ".xlsm") || (extension == ".xltx") || (extension == ".xltm"))
                        {
                            embeddingPartString = "/xl/embeddings/";
                        }
                        else
                        {
                            embeddingPartString = "/ppt/embeddings/";
                        }

                        // Get the embedded files names. 
                        foreach (PackagePart pkgPart in pkg.GetParts())
                        {
                            if (pkgPart.Uri.ToString().StartsWith(embeddingPartString))
                            {
                                string fileName1 = pkgPart.Uri.ToString().Remove(0, embeddingPartString.Length);
                                chkdLstEmbeddedFiles.Add(fileName1);
                            }
                        }
                        //pkg.Close();
                        if (chkdLstEmbeddedFiles.Count == 0)
                            //MessageBox.Show("The file does not contain any embedded files."); 

                            // Open the package and loop through parts  
                            // Check if the part uri to find if it contains the selected items in checked list box 
                            pkg = Package.Open(filepath);
                        foreach (PackagePart pkgPart in pkg.GetParts())
                        {
                            for (int i = 0; i < chkdLstEmbeddedFiles.Count; i++)
                            {
                                object chkditem = chkdLstEmbeddedFiles[i];

                                if (pkgPart.Uri.ToString().Contains(embeddingPartString + chkdLstEmbeddedFiles[i].ToString()))
                                {
                                    // Get the file name 
                                    string fileName1 = pkgPart.Uri.ToString().Remove(0, embeddingPartString.Length);

                                    // Get the stream from the part 
                                    System.IO.Stream partStream = pkgPart.GetStream();
                                    //string filePath = txtDestinationFolder.Text + "\\" + fileName1;

                                    // Write the steam to the file. 
                                    Environment.GetEnvironmentVariable("userprofile");
                                    System.IO.FileStream writeStream = new System.IO.FileStream(@"C:\Users\jyshet\Documents\MicrosoftDoc_File.docx", FileMode.Create, FileAccess.Write);
                                    ReadWriteStream(pkgPart.GetStream(), writeStream);

                                    // If the file is a structured storage file stored as a oleObjectXX.bin file 
                                    // Use Ole10Native class to extract the contents inside it. 
                                    if (fileName1.Contains("oleObject"))
                                    {
                                        // The Ole10Native class is defined in Ole10Native.cs file 
                                        // Ole10Native.ExtractFile(filePath, txtDestinationFolder.Text);
                                    }
                                }
                            }

                        }
                        pkg.Close();

我只能下载 doc 文件,其余部分看不到 txt 和 mht 文件。请建议在这种情况下该怎么做。我的问题是嵌入在 excel 中的任何对象都应该通过代码下载并保存在目标中。

4

0 回答 0