您的位置:首页 > 其它

DataSet导出到Excel比较完整的解决方案(二)--服务器端生成文件(downmoon)

2011-08-10 11:58 531 查看

DataSet导出到Excel比较完整的解决方案(二)--服务器端生成文件(downmoon)

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

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

public static void DataSetToLocalExcel(DataSet dataSet, string outputPath, bool deleteOldFile)
配置 DCOM 中 EXCEL 应用程序:
要在交互式用户帐户下设置 Office 自动化服务器,请按照下列步骤操作:
1. 以管理员身份登录到计算机,并使用完整安装来安装(或重新安装)Office。为了实现系统的可靠性,建议您将 Office CD-ROM 中的内容复制到本地驱动器并从此位置安装 Office。
2. 启动要自动运行的 Office 应用程序。这会强制该应用程序进行自我注册。
3. 运行该应用程序后,请按 Alt+F11 以加载 Microsoft Visual Basic for Applications (VBA) 编辑器。这会强制 VBA 进行初始化。
4. 关闭应用程序,包括 VBA。
5. 单击开始,单击运行,然后键入 DCOMCNFG。选择要自动运行的应用程序。应用程序名称如下所示:
Microsoft Access 97 - Microsoft Access 数据库
Microsoft Access 2000/2002 - Microsoft Access 应用程序
Microsoft Excel 97/2000/2002 - Microsoft Excel 应用程序
Microsoft Word 97 - Microsoft Word Basic
Microsoft Word 2000/2002 - Microsoft Word 文档
单击属性打开此应用程序的属性对话框。
6. 单击安全选项卡。验证使用默认的访问权限和使用默认的启动权限已选中。
7. 单击标识选项卡,然后选择交互式用户。
8. 单击确定,关闭属性对话框并返回主应用程序列表对话框。
9. 在 DCOM 配置对话框中,单击默认安全性选项卡。
10. 单击访问权限的编辑默认值。验证访问权限中是否列出下列用户,如果没有列出,则添加这些用户:
SYSTEM
INTERACTIVE
Everyone
Administrators
IUSR_ <machinename> *
IWAM_ <machinename> *
* 这些帐户仅在计算机上安装了 Internet Information Server (IIS) 的情况下才存在。
11. 确保允许每个用户访问,然后单击确定。
12. 单击启动权限的编辑默认值。验证启动权限中是否列出下列用户,如果没有列出,则添加这些用户:
SYSTEM
INTERACTIVE
Everyone
Administrators
IUSR_ <machinename> *
IWAM_ <machinename> *
* 这些帐户仅在计算机上安装有 IIS 的情况下才存在。
13. 确保允许每个用户访问,然后单击确定。
14. 单击确定关闭 DCOMCNFG。
如果你之前起用了身份模拟 (在 web.config 中配置了 <identity impersonate= "true "/> ) ,需要删除之!15.更新安装office,把.net可编程组件安装到本机(excel组件)
如果还是不行.干脃把交互式用户 换成"启动用户"

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

Reading and Writing Excel using OLEDB

主要的类文件如下:

public class ExcelReader : IDisposable
public static string path = @"TempExcel\STemp.xls";
public static string path2 = "TestUser.xls";
public static string PreFilePath = @"C:\Excel\";
public static void DataSetToLocalExcel(DataSet ds, string srcPath, string outputPath, bool deleteOldFile)
private DataSet Get_AllPrices()

DataSet dsPrice = new DataSet();
protected void btnGetData_Click(object sender, EventArgs e)
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:"MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=2";

附上两个方法: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐