1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Diagnostics;
6
using System.Web;
7
using System.Web.Services;
8
using System.Web.UI.WebControls;
9
using CrystalDecisions.Shared;
10
using CrystalDecisions.CrystalReports.Engine;
11
using System.IO;
12
using System.Data.OleDb;
13
using System.Web.UI;
14
15
namespace WorkWebService
16

{
17
/**//// <summary>
18
/// PrintService 的摘要说明。
19
/// </summary>
20
///
21
[WebService (Namespace="http://LocalHost/WorkWebService/", Description="A service displaying catalogs of Deepthoughts Publications")]
22
public class PrintService : System.Web.Services.WebService
23
24
{
25
private CrystalDecisions.CrystalReports.Engine.ReportDocument ReportDoc;
26
private TableLogOnInfo logOnInfo;
27
private DiskFileDestinationOptions FileOPS;
28
private ExportOptions ExOPS;
29
public PrintService()
30
{
31
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
32
InitializeComponent();
33
ReportDoc=new ReportDocument();
34
logOnInfo=new TableLogOnInfo();
35
FileOPS=new DiskFileDestinationOptions();
36
37
}
38
39
组件设计器生成的代码#region 组件设计器生成的代码
40
41
//Web 服务设计器所必需的
42
private IContainer components = null;
43
44
/**//// <summary>
45
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
46
/// 此方法的内容。
47
/// </summary>
48
private void InitializeComponent()
49
{
50
}
51
52
/**//// <summary>
53
/// 清理所有正在使用的资源。
54
/// </summary>
55
protected override void Dispose( bool disposing )
56
{
57
if(disposing && components != null)
58
{
59
components.Dispose();
60
}
61
base.Dispose(disposing);
62
}
63
64
#endregion
65
66
// WEB 服务示例
67
// HelloWorld() 示例服务返回字符串 Hello World
68
// 若要生成,请取消注释下列行,然后保存并生成项目
69
// 若要测试此 Web 服务,请按 F5 键
70
71
[WebMethod]
72
public string HelloWorld()
73
{
74
return "Hello World";
75
}
76
77
/**//// <summary>
78
/// 导出报表文件为PDF格式
79
/// </summary>
80
/// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
81
/// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
82
/// <param name="PDFFileName">你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的</param>
83
/// <returns>bool型,成功返回true,失败返回false</returns>
84
[WebMethod ( Description="A service displaying catalogs of Deepthoughts Publications")]
85
public bool ExportToPDF(string ReportFile,object ReportDataSource,string PDFFileName)
86
{
87
try
88
{
89
ReportDoc.Load(ReportFile);
90
ReportDoc.SetDataSource(ReportDataSource);
91
FileOPS.DiskFileName=PDFFileName;
92
ExOPS=ReportDoc.ExportOptions;
93
ExOPS.DestinationOptions=FileOPS;
94
ExOPS.ExportDestinationType=CrystalDecisions.Shared.ExportDestinationType.DiskFile;
95
ExOPS.ExportFormatType=CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
96
ReportDoc.Export();
97
return true;
98
}
99
catch
100
{
101
return false;
102
}
103
}
104
105
/**//// <summary>
106
/// 返回PDF文件到用户的IE浏览器中
107
/// </summary>
108
/// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
109
/// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
110
/// <param name="page">用于显示PDF WebForm</param>
111
/// <returns></returns>
112
[WebMethod ( Description="A service displaying catalogs of Deepthoughts Publications")]
113
public bool ReturnPDF(string ReportFile,object ReportDataSource,System.Web.UI.Page page)
114
{
115
int temp;
116
temp=System.Convert.ToInt32(System.DateTime.Now.Millisecond.ToString());
117
System.Random ra=new System.Random(temp);
118
int TmpNumber=ra.Next();
119
string TmpPDFFileName="c:\\"+System.Convert.ToString(TmpNumber)+".pdf";
120
if (ExportToPDF(ReportFile,ReportDataSource,TmpPDFFileName)==true)
121
{
122
page.Response.ClearContent();
123
page.Response.ClearHeaders();
124
page.Response.ContentType="application/pdf";
125
page.Response.WriteFile(TmpPDFFileName);
126
page.Response.Flush();
127
page.Response.Close();
128
System.IO.File.Delete(TmpPDFFileName);
129
return true;
130
} else
131
{
132
return false;
133
}
134
}
135
/**//// <summary>
136
/// 导出报表文件为xls格式
137
/// </summary>
138
/// <param name="ReportFile">报表文件名称,调用时请使用Server.MapPath("报表文件.rpt")这样来给这个参数</param>
139
/// <param name="ReportDataSource">报表文件所使用的数据源,是一个Dataset</param>
140
/// <param name="XLSFileName">你要导成的目标文件名称,注意不要放在wwwroot等目录中,iis会不让你导出的</param>
141
/// <returns>bool成功返回true,失败返回false</returns>
142
[WebMethod ( Description="A service displaying catalogs of Deepthoughts Publications")]
143
public bool ExportToXLS(string ReportFile,object ReportDataSource,string XLSFileName)
144
{
145
try
146
{
147
ReportDoc.Load(ReportFile);
148
ReportDoc.SetDataSource(ReportDataSource);
149
FileOPS.DiskFileName=XLSFileName;
150
ExOPS=ReportDoc.ExportOptions;
151
ExOPS.DestinationOptions=FileOPS;
152
ExOPS.ExportDestinationType=CrystalDecisions.Shared.ExportDestinationType.DiskFile;
153
ExOPS.ExportFormatType=CrystalDecisions.Shared.ExportFormatType.Excel;
154
ReportDoc.Export();
155
return true;
156
}
157
catch
158
{
159
return false;
160
}
161
}
162
[WebMethod ( Description="A service displaying catalogs of Deepthoughts Publications")]
163
public bool DataDownTOcsv(string sql,string filename,string tableheader1,string tableheader2,string columname,int columcount )
164
{
165
try
166
{
167
string strFileToOrg="",strBufferLine="",strBufferLine1="";
168
int i;
169
strFileToOrg = Server.MapPath("..\\pdf\\"+filename+".csv");
170
StreamWriter strmWriterObj=new StreamWriter(strFileToOrg,false,System.Text.Encoding.Default);//声明写入流对象
171
OleDbConnection OleDbConnection1=new OleDbConnection();
172
OleDbConnection1.ConnectionString="File Name="+HttpContext.Current.Server.MapPath("conn.udl");
173
OleDbConnection1.Open();
174
OleDbCommand cmdGenFile=new OleDbCommand();
175
cmdGenFile.Connection=OleDbConnection1;
176
cmdGenFile.CommandText=sql;
177
OleDbDataReader drGenFile=cmdGenFile.ExecuteReader();
178
strmWriterObj.WriteLine(tableheader1);
179
strmWriterObj.WriteLine(tableheader2);
180
strmWriterObj.WriteLine(columname);
181
while (drGenFile.Read())
182
{
183
strBufferLine="";
184
strBufferLine1=Convert.ToString(drGenFile.GetValue(0));
185
strBufferLine=strBufferLine1;
186
for (i=1;i<=(columcount-1);i++)
187
{
188
strBufferLine1="";
189
strBufferLine1=Convert.ToString(drGenFile.GetValue(i));
190
strBufferLine=strBufferLine+","+strBufferLine1;
191
}
192
strmWriterObj.WriteLine(strBufferLine);
193
}
194
strmWriterObj.Close();
195
drGenFile.Close();
196
return true;
197
}
198
catch
199
{
200
return false;
201
}
202
}
203
204
}
205
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16



17


18

19

20

21

22

23

24



25

26

27

28

29

30



31

32

33

34

35

36

37

38

39


40

41

42

43

44


45

46

47

48

49



50

51

52


53

54

55

56



57

58



59

60

61

62

63

64

65

66

67

68

69

70

71

72

73



74

75

76

77


78

79

80

81

82

83

84

85

86



87

88



89

90

91

92

93

94

95

96

97

98

99

100



101

102

103

104

105


106

107

108

109

110

111

112

113

114



115

116

117

118

119

120

121



122

123

124

125

126

127

128

129

130

131



132

133

134

135


136

137

138

139

140

141

142

143

144



145

146



147

148

149

150

151

152

153

154

155

156

157

158



159

160

161

162

163

164



165

166



167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182



183

184

185

186

187



188

189

190

191

192

193

194

195

196

197

198

199



200

201

202

203

204

205
