OpenOffice 根据报表模板导出报表
一.需求
由于公司要导出很多报表,而且要打印,报表格式时有变更
二.基本思路
1.动态模板+DataTable = 报表
三.程序设计思路
将模板存入数据库,导出报表时根据传入的DataTable和报表模板名称导出报表,为了提高运行效率在程序第一次运行时将报表模板导出到固定位置下次再用到该模板是不需再从数据库中导出,直接从固定位置读取模板。
1.方便变更报表格式和报表的制作,只需更改报表的模板重新上传到数据库中就可以,不用更改程序
2.基本上的常规报表都可以使用此程序
3.报表数据库脚本
Code
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Report_Tem_D]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Report_Tem_D]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Report_Tem_H]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Report_Tem_H]
GO
CREATE TABLE [dbo].[Report_Tem_D] (
[ReportID] [int] NOT NULL ,
[seqno] [int] NOT NULL ,
[Content] [varchar] (5000) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[rowguid] uniqueidentifier ROWGUIDCOL NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Report_Tem_H] (
[ReportID] [int] NOT NULL ,
[Type] [varchar] (50) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[ReportName] [varchar] (50) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[rowguid] uniqueidentifier ROWGUIDCOL NULL ,
[parameter] [varchar] (3000) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL
) ON [PRIMARY]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Report_Tem_D]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Report_Tem_D]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Report_Tem_H]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Report_Tem_H]
GO
CREATE TABLE [dbo].[Report_Tem_D] (
[ReportID] [int] NOT NULL ,
[seqno] [int] NOT NULL ,
[Content] [varchar] (5000) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[rowguid] uniqueidentifier ROWGUIDCOL NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Report_Tem_H] (
[ReportID] [int] NOT NULL ,
[Type] [varchar] (50) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[ReportName] [varchar] (50) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
[rowguid] uniqueidentifier ROWGUIDCOL NULL ,
[parameter] [varchar] (3000) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL
) ON [PRIMARY]
GO
4.上传报表模板到数据库
Code
/// <summary>
///将将报表模板存入数据库
/// </summary>
/// <param name="reportName">模板名称</param>
/// <param name="reportType">模板类型</param>
/// <returns></returns>
private static void UpTemplate(string reportFilePath,string reportType)
{
string reportFilePath = reportFilePath;
if (string.IsNullOrEmpty(reportFilePath) == true)
new System.Exception("报表存放路径不能为空");
string[] strArray = reportFilePath.Split(@"\".ToCharArray());
string reportName = strArray[strArray.Length];
int num2 = 0;//D表中序号
string str5 = ""; //sql 语句
SqlConnection connection = new SqlConnection(Leasear.Common.Infomation.ConnectionString);
connection.Open();//打开链接
SqlCommand command = null;
SqlDataReader reader = null;
command = new SqlCommand(("select reportname from report_tem_h where reportname='" + reportName.Replace("'", "''") + "' and type = '" + reportType + "';") + "select isnull(max(reportid),0) from report_tem_h;", connection);
reader = command.ExecuteReader();
reader.Read();
//检查报表是否已经存在
try
{
if (reader.HasRows)
{
new System.Exception("此文件已存在系統中,不可以上傳");
}
else
{
reader.NextResult(); //当读取批处理 Transact-SQL 语句的结果时,使数据读取器前进到下一个结果。
reader.Read();
int num = Convert.ToInt32(reader[0]) + 1;// ID + 1
reader.Close();
int count = 600;//buffer的长度
byte[] buffer = new byte[count];
FileStream input = new FileStream(reportFilePath, FileMode.Open);//打开报表模板reportFilePath
BinaryReader reader2 = new BinaryReader(input);//二进制读取模板
string str6 = "";//临时变量
str5 = string.Concat(new object[] { "insert report_tem_h (reportid,type,reportname,parameter) values (", num, ",'", reportType, "','", reportName.Replace("'", "''"), "','", "", "')" });
int num5 = input.Length/count;
//int num5 = Convert.ToInt32(num4);
num2 = 0;
while (num2 < num5)
{
reader2.Read(buffer, 0, count);
str6 = Convert.ToBase64String(buffer);
str5 = string.Concat(new object[] { (object)str5, "insert report_tem_d (reportid,seqno,content) values(", num, ",", num2, ",'", str6.Replace("'", "''"), "')" });
num2++;
}
count = Convert.ToInt32((long)(input.Length - (num5 * count)));
buffer = new byte[count];
reader2.Read(buffer, 0, count);
str6 = Convert.ToBase64String(buffer);//转换为字符串
//存入数据库
new SqlCommand(string.Concat(new object[] { (object)str5, "insert report_tem_d (reportid,seqno,content) values(", num, ",", num2, ",'", str6.Replace("'", "''"), "')" }), connection).ExecuteNonQuery();
input.Close();
reader2.Close();
//MessageBox.Show("已完成上傳");
//EventArgs args = new EventArgs();
//this.comboBox1_SelectedIndexChanged(this, args);
}
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
/// <summary>
///将将报表模板存入数据库
/// </summary>
/// <param name="reportName">模板名称</param>
/// <param name="reportType">模板类型</param>
/// <returns></returns>
private static void UpTemplate(string reportFilePath,string reportType)
{
string reportFilePath = reportFilePath;
if (string.IsNullOrEmpty(reportFilePath) == true)
new System.Exception("报表存放路径不能为空");
string[] strArray = reportFilePath.Split(@"\".ToCharArray());
string reportName = strArray[strArray.Length];
int num2 = 0;//D表中序号
string str5 = ""; //sql 语句
SqlConnection connection = new SqlConnection(Leasear.Common.Infomation.ConnectionString);
connection.Open();//打开链接
SqlCommand command = null;
SqlDataReader reader = null;
command = new SqlCommand(("select reportname from report_tem_h where reportname='" + reportName.Replace("'", "''") + "' and type = '" + reportType + "';") + "select isnull(max(reportid),0) from report_tem_h;", connection);
reader = command.ExecuteReader();
reader.Read();
//检查报表是否已经存在
try
{
if (reader.HasRows)
{
new System.Exception("此文件已存在系統中,不可以上傳");
}
else
{
reader.NextResult(); //当读取批处理 Transact-SQL 语句的结果时,使数据读取器前进到下一个结果。
reader.Read();
int num = Convert.ToInt32(reader[0]) + 1;// ID + 1
reader.Close();
int count = 600;//buffer的长度
byte[] buffer = new byte[count];
FileStream input = new FileStream(reportFilePath, FileMode.Open);//打开报表模板reportFilePath
BinaryReader reader2 = new BinaryReader(input);//二进制读取模板
string str6 = "";//临时变量
str5 = string.Concat(new object[] { "insert report_tem_h (reportid,type,reportname,parameter) values (", num, ",'", reportType, "','", reportName.Replace("'", "''"), "','", "", "')" });
int num5 = input.Length/count;
//int num5 = Convert.ToInt32(num4);
num2 = 0;
while (num2 < num5)
{
reader2.Read(buffer, 0, count);
str6 = Convert.ToBase64String(buffer);
str5 = string.Concat(new object[] { (object)str5, "insert report_tem_d (reportid,seqno,content) values(", num, ",", num2, ",'", str6.Replace("'", "''"), "')" });
num2++;
}
count = Convert.ToInt32((long)(input.Length - (num5 * count)));
buffer = new byte[count];
reader2.Read(buffer, 0, count);
str6 = Convert.ToBase64String(buffer);//转换为字符串
//存入数据库
new SqlCommand(string.Concat(new object[] { (object)str5, "insert report_tem_d (reportid,seqno,content) values(", num, ",", num2, ",'", str6.Replace("'", "''"), "')" }), connection).ExecuteNonQuery();
input.Close();
reader2.Close();
//MessageBox.Show("已完成上傳");
//EventArgs args = new EventArgs();
//this.comboBox1_SelectedIndexChanged(this, args);
}
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
connection.Close();
}
}
5.从数据库导出报表模板
6.删除报表模板
Excel版本基本已经实现:http://www.cnblogs.com/Tonyyang/archive/2007/11/23/970178.html
欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/