C#怎么把Ilist中的内容转换为byte[],最近在做一个npoi导出excel文件中的图片问题,感觉挺麻烦的,谢谢大家~~~

解决方案 »

  1.   

    List 里的元素可序列化不?直接用 BinaryFormatter 序列化成 byte[]
      

  2.   

    FileStream fs = new FileStream("SerializedDate.data", FileMode.Create); 
    BinaryFormatter bf = New BinaryFormatter(); 
    MyClass mc = new MyClass(); 
    bf.Serialize(fs, mc); 
    fs.Close(); 参考
      

  3.   

    HSSFWorkbook wb=new SSFWorkbook(newFileStream(Server.MapPath("App_Data/test.xls"),FileMode.Open));
    读excel;
    IList pictureList = wb.GetAllPictures(); 
    取图片信息;下面我不会了
      

  4.   

    /* ====================================================================
       Licensed to the Apache Software Foundation (ASF) under one or more
       contributor license agreements.  See the NOTICE file distributed with
       this work for additional information regarding copyright ownership.
       The ASF licenses this file to You under the Apache License, Version 2.0
       (the "License"); you may not use this file except in compliance with
       the License.  You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.
    ==================================================================== *//* ================================================================
     * Author: Tony Qu 
     * Author's email: tonyqus (at) gmail.com 
     * NPOI HomePage: http://www.codeplex.com/npoi
     * Contributors:
     * 
     * ==============================================================*/using System;
    using System.Collections.Generic;
    using System.Text;using System.IO;
    using NPOI.HSSF.UserModel;
    using NPOI.HPSF;
    using NPOI.POIFS.FileSystem;
    using NPOI.SS.UserModel;
    using System.Collections;
    using System.Drawing;namespace ExtractPicturesFromXls
    {
        class Program
        {
            static void Main(string[] args)
            {
                InitializeWorkbook();            IList pictures = hssfworkbook.GetAllPictures();
                int i = 0;
                foreach (HSSFPictureData pic in pictures)
                {
                    string ext = pic.SuggestFileExtension();
                    if (ext.Equals("jpeg"))
                    {
                        Image jpg = Image.FromStream(new MemoryStream(pic.Data));
                        jpg.Save(string.Format("pic{0}.jpg",i++));
                    }
                    else if (ext.Equals("png"))
                    {
                        Image png = Image.FromStream(new MemoryStream(pic.Data));
                        png.Save(string.Format("pic{0}.png", i++));
                    }            }        }        static HSSFWorkbook hssfworkbook;
            static void InitializeWorkbook()
            {
                FileStream file = new FileStream(@"clothes.xls", FileMode.Open, FileAccess.Read);
                hssfworkbook = new HSSFWorkbook(file);
            }
        }
    }
      

  5.   

    你的IList里面存放的是什么?
      

  6.   

    XSSFPictureData pic = (XSSFPictureData)pictures[i - 1];
                    string ext = pic.SuggestFileExtension();
                    if (ext.Equals("jpeg"))
                    {
                        System.Drawing.Image jpg = System.Drawing.Image.FromStream(new MemoryStream(pic.Data));
    jpg.Save(string.Format("pic{0}.jpg",i++));
    }