zoukankan      html  css  js  c++  java
  • DataSet导出到Excel比较完整的解决方案(二)服务器端生成文件(downmoon)

    前一篇文章中,介绍了DataSet导出到Excel时客户端生成文件的几种思路,接着往下说,服务器端生成文件,用户直接下载,应该格式是可以保证的!

    于是直接调用Excel的API生成。代码如下:

    DataSetToLocalExcel

    说明下,其中的  xlsApp.Application.DisplayAlerts   =   false;  的作用是不显示确认对话框    

    也可以逐Cell读取,那样可能会慢。本方法速度还过得去。

    生成Winform代码测试没错,部署时,以为只要引用两个dll就可以了

    Microsoft.Office.Interop.Excel.dll

    Office.dll


    那成想,问题接着来了,当在WebForm下调用时, 提示“检索   COM   类工厂中   CLSID   为   {00024500-0000-0000-C000-000000000046}   的组件时失败,原因是出现以下错误:   8000401a

    晕! Google下,解决方案是在服务器上安装Office,并配置DCOM权限。步骤如下:

    Code

     
    折腾了一番,总算可以用了!·只是服务器上装Office总感觉不爽,于是再尝试下别的方法:

    Reading and Writing Excel using OLEDB

    主要的类文件如下:
    ExcelReaderClass

    思路:通过读出Excel模板文件到DataTale,再把数据填充到DataTable,文件另存下就OK了!

     调用代码如下:

    DataSetToLocalExcel

     

    这里有点强调下:OleDbConnection特别要注意, 刚开始用http://www.connectionstrings.com/excel

      提供的标准串:

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:"MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";

    结果提示:“操作必须使用一个可更新的查询”。因为读取结果正常,以为是excel没有写权限所致,增加了相应权限后,结果依然如故。这下火了! Google下, 有解决方案

    http://www.cnblogs.com/richinger/archive/2008/09/28/1301170.html

    A: HDR ( HeaDer Row )设置
        若指定值为Yes,代表 Excel 档中的工作表第一行是栏位名称

        若指定值為 No,代表 Excel 档中的工作表第一行就是資料了,沒有栏位名称

        B:IMEX ( IMport EXport mode )设置

         IMEX 有三种模式,各自引起的读写行为也不同,容後再述:
         0 is Export mode
         1 is Import mode
         2 is Linked mode (full update capabilities)

        

    于是修改为:

    Code

     附上两个方法:StringParse和ShortParse

    代码
      #region String

            
    public static string EmptyString = string.Empty;
            
    public static string StringParse(string old)
            { 
    return StringParse(old, string.Empty); }
            
    public static string StringParse(object old)
            { 
    return StringParse(old, string.Empty); }
            
    public static string StringParse(object old, string ReplaceString)
            {
                
    if (old == null || old.ToString().Trim().Length == 0)
                {
                    
    if (ReplaceString == null || ReplaceString.Trim().Length == 0) { return string.Empty; }
                    
    else { return ReplaceString.Trim(); }
                }
                
    else { return old.ToString().Trim(); }
            }
            
    public static string StringParse(string old, string ReplaceString)
            {
                
    if (old == null || old.Trim().Length == 0)
                {
                    
    if (ReplaceString == null || ReplaceString.Trim().Length == 0) { return string.Empty; }
                    
    else { return ReplaceString.Trim(); }
                }
                
    else { return old.Trim(); }
            }
            
    #endregion

     
    #region Short
            
    public static short ShortParse(string old)
            { 
    return ShortParse(old, 0); }
            
    public static short ShortParse(object old)
            { 
    return ShortParse(old, 0); }
            
    public static short ShortParse(string old, short NullValue)
            {
                
    short i = 0;
                
    try
                {
                    
    if (old != null && old.ToString().IndexOf('.'> 0)
                    {
                        
    string str = old.ToString().Remove(old.ToString().IndexOf('.'));
                        i 
    = short.Parse(str.Trim());
                    }
                    
    else { i = short.Parse(old.ToString().Trim()); }

                }
                
    catch { try { i = NullValue; } catch { i = (short)0; } }
                
    return i;
            }
            
    public static short ShortParse(object old, short NullValue)
            {
                
    short i = 0;
                
    try
                {
                    
    if (old != null && old.ToString().IndexOf('.'> 0)
                    {
                        
    string str = old.ToString().Remove(old.ToString().IndexOf('.'));
                        i 
    = short.Parse(str.Trim());
                    }
                    
    else { i = short.Parse(old.ToString().Trim()); }
                }
                
    catch { try { i = NullValue; } catch { i = (short)0; } }
                
    return i;
            }
            
    public static short ShortTryParse(object srcObj)
            {
                
    short defaultValue;
                
    if (srcObj == null) { return 0; }
                Int16.TryParse(srcObj.ToString(), 
    out defaultValue);
                
    return defaultValue;
            }
            
    public static short ShortTryParse(object srcObj, short NullValue)
            {
                
    short defaultValue;
                
    ////if (srcObj == null) { return 0; }
                Int16.TryParse(srcObj.ToString(), out defaultValue);
                
    if (!Int16.TryParse(srcObj.ToString(), out defaultValue)) { Int16.TryParse(NullValue.ToString(), out defaultValue); }
                
    return defaultValue;
            }
            
    #endregion
     
    邀月注:本文版权由邀月和博客园共同所有,转载请注明出处。
    助人等于自助!  3w@live.cn
  • 相关阅读:
    10、Python的while与死循环
    8、 Python的if分支练习题
    7、 Python中的if多重判断
    6、Python的if判断和两重判断
    5、运算符
    4、数据类型:字典
    placeholder 颜色更改
    禁止video在苹果手机上的自动全屏播放
    点击label出发两次点击事件
    instanceof 和 typeof
  • 原文地址:https://www.cnblogs.com/downmoon/p/1376702.html
Copyright © 2011-2022 走看看