C#中做报表除了用水晶报表还能用什么做 ?
感觉水晶报表打开很慢 , 第一次打开一张报表要10秒左右 。
[解决办法]
报表不是很复杂的话可以用PrintDocument,需要写代码
我自己正好在写一个模板打印的代码
public class CustomPrinter
{
public float LineBetween = 1;
private string TempletName;
private DataTable Table;
private Font DefualtFont = new Font( "楷体_GB2312 ", 16);
private string txt;
public PrintDocument printDocument;
private float yPosition;
private int currentPage = 0;
public CustomPrinter()
{
}
public CustomPrinter(string templteName, DataTable table)
{
TempletName = templteName;
Table = table;
printDocument = new PrintDocument();
printDocument.PrintController = new StandardPrintController();
printDocument.PrintPage += new PrintPageEventHandler(Print);
printDocument.BeginPrint += new PrintEventHandler(BeginPrint);
}
private void BeginPrint(object sender, PrintEventArgs e)
{
currentPage = 0;
txt = ExplainText();
}
private void Print(object sender, PrintPageEventArgs e)
{
yPosition = e.MarginBounds.Top;
if (currentPage == 0)
{
Font titleFont = new Font( "黑体 ", DefualtFont.Size + 2);
float xTitle = e.MarginBounds.Left + (e.MarginBounds.Width - e.Graphics.MeasureString(TempletName, titleFont).Width) / 2;
e.Graphics.DrawString(TempletName, titleFont, Brushes.Black, xTitle, yPosition);
yPosition += e.Graphics.MeasureString(TempletName, titleFont).Height * LineBetween;
}
PrintJustLine(e);
}
private string ExplainText()
{
string newtxt = " ";
txt = File.ReadAllText(Path.Combine(Application.StartupPath, string.Format(@ "{0}\{1}.TML ", "Templet ", TempletName)), Encoding.Default);
string[] txtlist = txt.Split(new char[] { '{ ', '} ' });
for (int i = 0; i < txtlist.Length; i++)
{
txtlist[i] = GetValue(txtlist[i]);
newtxt += txtlist[i];
}
return newtxt;
}
private void SetFormat(string field)
{
try
{
string[] format = field.Split(new char[] { '= ' });
if (format[0].ToUpper().Equals( "FontName ".ToUpper())) DefualtFont = new Font(format[1], DefualtFont.Size);
if (format[0].ToUpper().Equals( "FontSize ".ToUpper())) DefualtFont = new Font(DefualtFont.Name, float.Parse(format[1]));
if (format[0].ToUpper().Equals( "Between ".ToUpper())) LineBetween = float.Parse(format[1]);
}
catch { }
}
private string GetValue(string field)
{
if (field.IndexOf( "= ") > 0)
{
SetFormat(field);
return " ";
}
string value = field;
if (Table.Columns[field] != null)
{
value = Table.Rows[0][field].ToString();
}
else
{
switch (field)
{
case "ChequeId ":
value = LightningData.Lightning.ShowInputBox( "请输入支票编号 ");
value = value.Length == 0 ? " " : string.Format( ",支票编号为:{0} ", value);
break;
case "BigMoney ":
LightningData.NumberToRMB rmb = new LightningData.NumberToRMB();
value = rmb.NumToRMBStr(Table.Rows[0][ "Money "]);
break;
case "FeeRate ":
value = LightningData.LDBM.GetFeeRate(Table.Rows[0][ "Grade "].ToString()).ToString();
break;
case "NeedMoney ":
decimal area = 0.0M;
if (Table.Rows[0][ "Area "] != DBNull.Value) area = Convert.ToDecimal(Table.Rows[0][ "Area "]);
value = LightningData.LDBM.GetCurrentFee(Table.Rows[0][ "Grade "].ToString(), area).ToString();
break;
case "Date ":
value = DateTime.Now.ToLongDateString();
break;
}
}
return value;
}
private void PrintJustLine(PrintPageEventArgs e)
{
int charactersFitted;
int linesFilled;
SizeF stringSize = new SizeF();
SizeF layoutSize = new SizeF(e.MarginBounds.Width, DefualtFont.GetHeight(e.Graphics) * 1.2f);
while (txt.Length > 0)
{
stringSize = e.Graphics.MeasureString(txt, DefualtFont, layoutSize, new StringFormat(), out charactersFitted, out linesFilled);
RectangleF rf = new RectangleF(new PointF(e.MarginBounds.Left, yPosition), layoutSize);
e.Graphics.DrawString(txt.Substring(0, charactersFitted), DefualtFont, Brushes.Black, rf, new StringFormat());
txt = txt.Remove(0, charactersFitted);
yPosition += stringSize.Height * LineBetween;
if ((yPosition + stringSize.Height) > e.MarginBounds.Bottom)
{
currentPage++;
e.HasMorePages = true;
return;
}
else e.HasMorePages = false;
}
}
}
}
[解决办法]
FastReport Studio,原来DELPHI里的FastReport,这是她的.NET版,感觉不错,功能不比水晶差,有不少比水晶还强,而且体积小。