我采用不通过OLE生成excel文件的方法生成execl,可是解压后重新计算格式时,出错“无法打开档案” 
有的单元格字符很长,有的1000多字符,甚至更多所以,请高手帮忙修改 
    string timeFlag = DateTime.Now.ToFileTime().ToString(); 
            string dirStr = path + DateTime.Now.ToFileTime().ToString(); 
            System.IO.Directory.CreateDirectory(dirStr); 
            int fileNum = 1; 
            //不通过OLE生成excel文件的方法          
            for (int index = 0; index < list.Count; index++) 
            { 
                ProblemCodeExcel excel = new ProblemCodeExcel(GenFileName(dirStr + "\\")); 
                excel.BeginWrite(); 
                excel.WriteHeader(); 
              
                Console.WriteLine("正在生成第" + fileNum.ToString() + "个文档....."); 
                for (ushort row = 1; row < ushort.MaxValue && index < list.Count; row++, index++) 
                { 
              if (list[index] != null) 
                    { 
                        excel.WriteString(row, 0, list[index].CreateDate); 
                        excel.WriteString(row, 1, list[index].CaseId); 
                        excel.WriteString(row, 2, list[index].Country); 
                        excel.WriteString(row, 3, list[index].Datasource); 
                        excel.WriteString(row, 4, list[index].Engineer); 
                        excel.WriteString(row, 5, list[index].Model); 
                        excel.WriteString(row, 6, list[index].Problem); 
                        excel.WriteString(row, 7, list[index].ProblemCode); 
                        excel.WriteString(row, 8, list[index].ProblemDesc); 
                        excel.WriteString(row, 9, list[index].ProblemType); 
                        excel.WriteString(row, 10, list[index].ProblemTypeDesc); 
                        excel.WriteString(row, 11, list[index].ProductType); 
                        excel.WriteString(row, 12, list[index].Reply); 
                        excel.WriteString(row, 13, list[index].Series); 
                        excel.WriteString(row, 14, list[index].Server); 
                        excel.WriteString(row, 15, list[index].Subject); 
                        excel.WriteString(row, 16, list[index].SubSeries); 
                    } 
                    else 
                        break;                 } 
                Console.WriteLine("生成成功!"); 
            
                index--; 
                fileNum++;  
                excel.EndWrite(); 
            }             ZipDAO dao = new ZipDAO(dirStr); 
            dao.ZipFold(); 
            DeleteFile(dirStr); 
            Console.WriteLine("处理完毕"); 
            Thread.Sleep(1000); 
            return virtualPath + timeFlag + ".rar"; 
        }         private static string GenFileName(string phycialPath) 
        { 
            string timeFlag = DateTime.Now.ToFileTime().ToString(); 
            return phycialPath + timeFlag + ".xls"; 
        } public class ExcelWriter 
    { 
        System.IO.FileStream _wirter; 
        public ExcelWriter(string strPath) 
        { 
            _wirter = new System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate); 
        } 
        /// <summary> 
        /// 写入short数组 
        /// </summary> 
        /// <param name="values"> </param> 
        private void _writeFile(ushort[] values) 
        { 
            foreach (ushort v in values) 
            { 
                byte[] b = System.BitConverter.GetBytes(v); 
                _wirter.Write(b, 0, b.Length); 
            } 
        } 
        /// <summary> 
        /// 写文件头 
        /// </summary> 
        public void BeginWrite() 
        { 
            _writeFile(new ushort[] { 0x809, 8, 0, 0x10, 0, 0 }); 
        } 
        /// <summary> 
        /// 写文件尾 
        /// </summary> 
        public void EndWrite() 
        { 
            _writeFile(new ushort[] { 0xa, 0 }); 
            _wirter.Close(); 
        } 
        /// <summary> 
        /// 写一个数字到单元格x,y 
        /// </summary> 
        /// <param name="x"> </param> 
        /// <param name="y"> </param> 
        /// <param name="value"> </param> 
        public void WriteNumber(ushort x, ushort y, double value) 
        { 
            _writeFile(new ushort[] { 0x203, 14, x, y, 0 }); 
            byte[] b = System.BitConverter.GetBytes(value); 
            _wirter.Write(b, 0, b.Length); 
        } 
        /// <summary> 
        /// 写一个字符到单元格x,y 
        /// </summary> 
        /// <param name="x"> </param> 
        /// <param name="y"> </param> 
        /// <param name="value"> </param> 
        public void WriteString(ushort x, ushort y, string value) 
        { 
            byte[] b = System.Text.Encoding.Default.GetBytes(value); 
            _writeFile(new ushort[] { 0x204, (ushort)(b.Length + 8), x, y, 0, (ushort)b.Length }); 
            _wirter.Write(b, 0, b.Length); 
            
        } 
    } 

解决方案 »

  1.   

    可以引用Aspose.Excel.dll文件来操作Excel文件或取得数据生成Excel文件示例:using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using Aspose.Excel; //生成Excel文件,保存private void createExcel(string data){string filename = 'aaa';Excel excel = new Excel();Worksheets sheets = excel.WorkSheets;Cells cells = sheets[0].Cells;cells[0, 0].PutValue(data);excel.Save(Application.StartupPath+filename'.xls', FileFormatType.Default);}//这只是个简单的例子,可以对excel其它格式的操作。   //read excelprivate string readExcel(){ string filename ='..'//文件位置Excel excel = new Excel();Worksheets sheets = excel.WorkSheets;Cells cells = sheets[0].Cells;return  cells[0,0].Value.ToString();}
      

  2.   

    可以引用Aspose.Excel.dll文件来操作Excel文件或取得数据生成Excel文件示例:using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using Aspose.Excel; //生成Excel文件,保存private void createExcel(string data){string filename = 'aaa';Excel excel = new Excel();Worksheets sheets = excel.WorkSheets;Cells cells = sheets[0].Cells;cells[0, 0].PutValue(data);excel.Save(Application.StartupPath+filename'.xls', FileFormatType.Default);}//这只是个简单的例子,可以对excel其它格式的操作。   //read excelprivate string readExcel(){ string filename ='..'//文件位置Excel excel = new Excel();Worksheets sheets = excel.WorkSheets;Cells cells = sheets[0].Cells;return  cells[0,0].Value.ToString();}
      

  3.   

    有人没
    可以给我所有生成excel的方法,1 oledb 2 流 3其他
      

  4.   


    汗...找个dll,调用下方法就可以了。
      

  5.   

    在COM中调用Microsoft Excel X.0 Object Library就可以生成Excel
      

  6.   

    我需要使用oledb 的方法
    请贴上代码