2012-07-18 13 views
14

मेरे पास कुछ .XLSX फ़ाइलों को बनाने के लिए EPPlus का उपयोग करके एक अजीब समस्या है। मेरे पास एक पैकेज बनाया जा रहा है, और फिर प्रतिक्रिया के लिए आउटपुट है।ईपीप्लस त्रुटि जब आउटपुट .XLSX प्रतिक्रिया

मैं एक पैकेज बनाया के रूप में निम्नानुसार है:

var file = new FileInfo(@"C:\Test.xlsx"); 
ExcelPackage package = new ExcelPackage(file); 
//...code to output data...// 
package.Save(); 

यह मेरा स्थानीय सेल्सियस के लिए फ़ाइल की बचत होती है: सही ढंग से ड्राइव, और जब मैं इसे खोलने यह बहुत अच्छा काम करता है। कोई त्रुटि नहीं है या कुछ भी, स्वरूपण सही, आदि

हालांकि है, मैं अब उत्पादन के लिए प्रतिक्रिया स्ट्रीम करने के लिए इस फ़ाइल को इच्छा तो मैं इस तरह देखने के लिए कोड मैं था संशोधित किया है:

ExcelPackage package = new ExcelPackage(); 
//...code to output data...// 
MemoryStream result = new MemoryStream(); 
package.SaveAs(result); 
context.Response.Clear(); 
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";              
context.Response.AddHeader("Content-Disposition", "attachment;filename=MissionDetails.xlsx"); 
result.WriteTo(context.Response.OutputStream); 
context.Response.End(); 

लेकिन जब मैं चलाते हैं तो यह कोड मैं शीघ्र निम्नलिखित जब एक्सेल फ़ाइल खोलने का प्रयास:

Excel found unreadable content in filename.xlsx. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes

क्लिक करने से हाँ तो प्रदर्शित करता है शीघ्र निम्नलिखित:

This file cannot be opened by using Microsoft Excel. Do you want to search the Microsoft Office Online Web site for a converter that can open the file?

मैं No यहाँ चयन करें और फिर इसे Excel फ़ाइल और प्रदर्शित करता है इस त्रुटि को खोलता है:

Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.

लेकिन फ़ाइल तो ठीक लोड करता है और सही ढंग से और सब कुछ प्रारूप तैयार किया जाता प्रतीत होता है। लेकिन हर बार जब मैं फ़ाइल खोलने की कोशिश करता हूं तो यह वही संकेत और त्रुटि संदेश देता है।

Note: The code to output the data does not change for either of these scenarios.

क्या किसी ने ऐसा कुछ देखा है? या क्या कोई विचार है कि प्रतिक्रिया को आउटपुट करते समय फ़ाइल को गलत तरीके से केवल सहेजने का कारण क्या हो सकता है?

+0

क्या आप परीक्षण उद्देश्यों के लिए फ़ाइल की सामग्री को सरल बना सकते हैं (यानी केवल एक सेल का मान सेट करें) यह सुनिश्चित करने के लिए कि फ़ाइल स्ट्रीमिंग के विपरीत फ़ाइल की सामग्री में कुछ गड़बड़ नहीं है? – JustinStolle

+0

जैसा कि कहा गया है, सामग्री वही है जब मैं इसे हार्ड ड्राइव पर सहेजता हूं। और मैंने इसका परीक्षण किया है और सत्यापित किया है कि यह वह सामग्री नहीं है जिसे मैं सेल में जोड़ रहा हूं। प्रतिक्रिया के साथ यह कुछ मुद्दा होना चाहिए। – j00b

+0

तो समस्या को निहित करने के लिए स्ट्रीम करने के लिए स्ट्रीम करने के लिए एक बेहद सरल फ़ाइल बनाएं - यदि ऐसा है तो ईपीप्लस के साथ इसका कोई संबंध नहीं हो सकता है। – JustinStolle

उत्तर

0
WriteTo विधि के बजाय

, इस प्रयास करें:

context.Response.BinaryWrite(package.GetAsByteArray()); 
+1

मैंने कोशिश की और पाया कि फ़ाइल खोलने का प्रयास करते समय यह निम्न त्रुटि देता है: "एक्सेल फ़ाइल नहीं खोल सकता '(फ़ाइल नाम)' xlsx 'क्योंकि फ़ाइल प्रारूप या फ़ाइल एक्सटेंशन मान्य नहीं है। सत्यापित करें कि फ़ाइल नहीं है दूषित हो गया है और फ़ाइल एक्सटेंशन फ़ाइल के प्रारूप से मेल खाता है। " – j00b

+0

आप किस कार्यालय/एक्सेल का उपयोग कर रहे हैं? आपको एक संगतता पैक को अपग्रेड या उपयोग करने की आवश्यकता हो सकती है (देखें http://www.microsoft.com/en-us/download/details.aspx?id=3)। – JustinStolle

+0

मेरा मानना ​​है कि आपको एक्सेल 2007 या बाद में नए ओपन ऑफिस एक्सएमएल संगत प्रारूप को पढ़ने के लिए आवश्यकता होगी जिसे ईपीप्लस पैकेज बनाता है। – JustinStolle

13

मैं इस समस्या का समाधान मिल गया है! जैसा कि उम्मीद थी कि प्रतिक्रिया के साथ इसका कुछ संबंध था, क्योंकि स्थानीय रूप से सहेजे जाने पर मैं फ़ाइल खोलने में सक्षम था, लेकिन प्रतिक्रिया के माध्यम से नहीं।

यहां मुद्दा यह है कि मेरा कोड में लपेटा गया था। ब्लॉक को अपवादित किया गया था जहां अपवाद लॉग और प्रदर्शित किया गया था।

यह मेरा ध्यान गया है कि जब आप Response.End() एक System.Threading.ThreadAbortException उठाया है कहते हैं। जब यह उठाया जाता है, ऐसा लगता है कि त्रुटि का आउटपुट मेरी फाइल के अंत में जोड़ा जा रहा था।

जब मैं उस विशिष्ट अपवाद के लिए त्रुटि लॉगिंग से छुटकारा पाता हूं, तो यह बहुत अच्छा काम करता है!

अधिक जानकारी http://epplus.codeplex.com/discussions/223843?ProjectName=epplus

//...output code...// 
catch(Exception ex){ 
    if (!(ex is System.Threading.ThreadAbortException)) 
    { 
     //Log other errors here 
    } 
} 
+1

अपवाद को Response.Flush() पर उठाया गया है। मैं विधि को हटा देता हूं तो फ़ाइल बिना किसी त्रुटि के ठीक खुलती है। – user1131926

0

page.aspx के लिए इस पोस्ट को देखें।सीएस कोड जब निर्यात के लिए बटन पर क्लिक करने के लिए एक्सेल

string templateFileName = Server.MapPath("~/ReportingTemplate/test.xlsx"); 
       System.IO.FileInfo templateFile = new System.IO.FileInfo(templateFileName); 

       String message = ExcelPackagePlusLibrary.EPPlus.ExportToExcel(templateFile, dt, false, exportFileName, Response, "Data", "Summary", "Please type the client name here"); 
       if (String.IsNullOrEmpty(message) == false) 
       { 
        /* Exception occur. */ 
       } 
       dt.Clear(); 
/////////////////////////////////////////////////////////////////////////////////////// 

    /// <summary> 
      /// ExportToExcel is a method used for Export To Excel with template file. 
      /// 
      /// </summary> 
      /// <param name="templateFile">The fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character.</param> 
      /// <param name="dt">Datatable for export.</param> 
      /// <param name="printHeaders">Datatable's header used or not, when Export it. </param> 
      /// <param name="exportFileName">provide fileName only not path. </param> 
      /// <param name="Response">System.Web.HttpResponse. </param> 
      /// <param name="sheetNames">arg[0] means provide sheet name where you want to load data. \n (Optional Parameter) arg[1] means provide sheet name where you want to edit. (Optional Parameter) arg[2] means if your intention is to Edit sheet so provide searchText.</param> 
      /// 
      public static string ExportToExcel(FileInfo templateFile, DataTable dt, bool printHeaders, string exportFileName, System.Web.HttpResponse Response, params String[] sheetNames) 
      { 
       try 
       { 
        using (ExcelPackage p = new ExcelPackage(templateFile, false)) 
        { 
         EPPlus.AddSheetWithTemplate(p, dt, sheetNames[0], printHeaders); 


         String[] clientName = exportFileName.Split(new char[] { '_' }, 2); 

         if (sheetNames.Count() > 2) 
         { 
          ExcelPackagePlusLibrary.EPPlus.EditSheet(p, sheetNames[1], sheetNames[2], clientName[0] ?? exportFileName); 
         } 

         Byte[] fileBytes = p.GetAsByteArray(); //Read the Excel file in a byte array 

         //Clear the response 
         Response.ClearHeaders(); 
         Response.ClearContent(); 
         Response.Clear(); 

         //Response.Cookies.Clear(); 


         //Add the header & other information 
         //Response.Cache.SetCacheability(System.Web.HttpCacheability.Private); 
         //Response.CacheControl = "private"; 
         //Response.Charset = System.Text.UTF8Encoding.UTF8.WebName; 
         //Response.ContentEncoding = System.Text.UTF8Encoding.UTF8; 
         //Response.AppendHeader("Content-Length", fileBytes.Length.ToString()); 
         //Response.AppendHeader("Pragma", "cache"); 
         //Response.AppendHeader("Expires", "60"); 
         Response.AddHeader("Content-Disposition", 
         "attachment; " + 
         "filename=" + exportFileName + "; " + 
         "size=" + fileBytes.Length.ToString() + "; " + 
         "creation-date=" + DateTime.Now.ToString("R").Replace(",", "") + "; " + 
         "modification-date=" + DateTime.Now.ToString("R").Replace(",", "") + "; " + 
         "read-date=" + DateTime.Now.ToString("R").Replace(",", "")); 

         //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
         Response.ContentType = "application/x-msexcel"; 

         //Write it back to the client 
         Response.BinaryWrite(fileBytes); 
         Response.Flush(); 
         Response.Close(); 

         /* Download to Client Side. */ 
         //DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Testing/Downloaded/" + DateTime.Now.ToString("MM-dd-yyyy"))); 
         //if (!dir.Exists) 
         //{ 
         // dir.Create(); 
         //} 
         //File.WriteAllBytes(dir.FullName + "\\" + fileName, fileBytes); 

         return String.Empty; 
        } 
       } 
       catch (Exception ex) 
       { 
        _ErrorMessage = ex.Message.ToString(); 
        return _ErrorMessage; 
       } 
      } 
8

धन्यवाद अपने लिंक जूब मेरी समस्या यह बुला रहा था "GetAsByteArray()" soved। जैसा कि नीचे दिया गया है और आपके द्वारा दिए गए लिंक में, मैं अपवाद को अपवाद को रखने से रोकता हूं। कुछ मैजिक द्वारा।

आप वोट प्राप्त करते हैं और वोट देते हैं!

mrxrsd 
Editor 

Aug 17, 2010 at 12:30 PM 


Call response.clear before send stream back to client. 

        Response.Clear(); 
        Response.AddHeader("content-disposition", "attachment; filename=file.xlsx"); 
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";     
        Response.BinaryWrite(pck.GetAsByteArray()); 
        Response.End(); 

http://epplus.codeplex.com/discussions/223843?ProjectName=epplus

0

मैं EPPlus IE से खोलने के लिए नहीं मिल सका, यह ठीक बचाया और त्रुटियों के बिना खोला। (क्रोम दोनों को बचाने और खोलने के लिए ठीक काम किया)। यहां दिए गए सुझाव मेरे लिए काम नहीं करते थे - संभावित रूप से क्योंकि मैं एएसपीएक्स फ़ाइल का उपयोग कर रहा था, न कि एएसएक्स फाइल जो अंत में मेरे लिए काम करती थी।

खोज के घंटों के बाद मैं this stackoverflow post के अनुसार एक एएसएक्स फ़ाइल का उपयोग कर समाप्त हुआ और अब यह सभी ब्राउज़रों में ईपीप्लस का उपयोग कर चेतावनी/corupt फ़ाइल संदेशों के बिना काम कर रहा है।

अतिरिक्त संसाधन दूसरों की मदद कर सकते हैं कि:

मुझे आशा है कि यह किसी और कुछ समय बचाने के।