Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取excel usng C中嵌入的对象#_C#_Excel_Object_Embedded Resource_Packaging - Fatal编程技术网

C# 如何获取excel usng C中嵌入的对象#

C# 如何获取excel usng C中嵌入的对象#,c#,excel,object,embedded-resource,packaging,C#,Excel,Object,Embedded Resource,Packaging,我有一个应用程序,我需要获取嵌入excel中的对象,这些对象应该通过代码存储在某个位置 connection.Close(); //connection.ResetState(); string embeddingPartString; //ArrayList chkdLstEmbeddedFiles =

我有一个应用程序,我需要获取嵌入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();
connection.Close();
//connection.ResetState();
字符串嵌入部分字符串;
//ArrayList chkdLstEmbeddedFiles=新ArrayList();
List chkdLstEmbeddedFiles=new List();
//字符串文件名=txtSourceFile.Text;
如果(filepath==string.Empty | |!System.IO.File.Exists(filepath))
{
回来
}
//打开包文件
pkg=Package.Open(filepath,FileMode.Open,FileAccess.ReadWrite);
System.IO.FileInfo fi=新的System.IO.FileInfo(filepath);
字符串扩展名=fi.extension.ToLower();
if((扩展名=“.docx”)| |(扩展名=“.dotx”)| |(扩展名=“.docm”)| |(扩展名=“.dotm”))
{
embeddingPartString=“/word/embeddings/”;
}
else if((扩展名=“.xlsx”)| |(扩展名=“.xlsm”)| |(扩展名=“.xltx”)| |(扩展名=“.xltm”))
{
embeddingPartString=“/xl/embeddings/”;
}
其他的
{
embeddingPartString=“/ppt/embeddings/”;
}
//获取嵌入的文件名。
foreach(pkg.GetParts()中的PackagePart pkgPart)
{
if(pkgPart.Uri.ToString().StartsWith(embeddingPartString))
{
string fileName1=pkgPart.Uri.ToString().Remove(0,embeddingPartString.Length);
chkdLstEmbeddedFiles.Add(fileName1);
}
}
//pkg.Close();
if(chkdLstEmbeddedFiles.Count==0)
//Show(“该文件不包含任何嵌入文件”);
//打开包装并循环检查零件
//检查部件uri是否包含选中列表框中的选定项
pkg=Package.Open(filepath);
foreach(pkg.GetParts()中的PackagePart pkgPart)
{
对于(int i=0;i
我只能下载doc文件,其余的文件不像txt和mht文件。请建议在这种情况下该怎么办。我的问题是excel中嵌入的任何对象都应该通过代码下载并保存在目标中