如何打印出excel的内容?

weixinzhu 2004-11-12 12:22:54
DBControl.PathStr = Application.StartupPath.Trim();
try
{
DBControl.DestinationFileName = DBControl.PathStr + @"\Report.xls";
DBControl.SourceFileName = DBControl.PathStr + @"\wxREPORT.xls"; // GRJHM-BDG3W-9P6TY-FB4WX-DKF9Y
try
{
System.IO.File.Delete(DBControl.DestinationFileName);
File.Copy(DBControl.SourceFileName, DBControl.DestinationFileName);
}
catch(Exception ee)
{
MessageBox.Show(ee.Message);
return;
}

Excel.Application xlApp = new Excel.Application(); //注意一定要有New,这样才能有EXCEL进程的接口实例
Excel.Workbook xlBook = xlApp.Workbooks.Add(DBControl.DestinationFileName); //不要用new,这一句已经使xlBook指向了实例
Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets["分货类吞吐量"];
xlSheet.Activate();
xlSheet.Application.Visible = true;

//填报单位
DBControl.fillDsCompany();
for (int i=0;i<=DBControl.dsCompany.Tables[0].Rows.Count -1;i++)
{
if (DBControl.dsCompany.Tables[0].Rows[i]["companycode"].ToString()==DBControl.companyCode)
{
xlSheet.Cells[3,2]=DBControl.dsCompany.Tables[0].Rows[i]["companyname"].ToString();
}
}
//统计日期
xlSheet.Cells[3,8]=this.dtStartDate.Value.Year.ToString()+'/'+this.dtStartDate.Value.Month.ToString()+'/'+this.dtStartDate.Value.Day.ToString();
xlSheet.Cells[3,10]=this.dtEndDate.Value.Year.ToString()+'/'+this.dtEndDate.Value.Month.ToString()+'/'+this.dtEndDate.Value.Day.ToString();

//打印表体
for (int i=0;i<=ds.Tables[0].Rows.Count -1;i++)
{
for (int j=1;j<this.c1dbg.Columns.Count -1;j++)
{
xlSheet.Cells[6+i,j]=ds.Tables[0].Rows[i][j];
}
}
//单位负责人
xlSheet.Cells[ds.Tables[0].Rows.Count +7,1]="单位负责人:";
xlSheet.Cells[ds.Tables[0].Rows.Count +7,3]=this.txtFuzeren.Text;
//填报人
xlSheet.Cells[ds.Tables[0].Rows.Count +7,4]="填报人:";
xlSheet.Cells[ds.Tables[0].Rows.Count +7,5]=DBControl.UserName;
//填报日期
xlSheet.Cells[ds.Tables[0].Rows.Count +7,7]="填报日期:";
xlSheet.Cells[ds.Tables[0].Rows.Count +7,8]=System.DateTime.Today ;
//--------------------
Excel.Range r2=xlSheet.get_Range(xlSheet.Cells[6,1],xlSheet.Cells[ds.Tables[0].Rows.Count+5,10]);

r2.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = 7;
r2.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = 7;
r2.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = 7;
r2.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = 7;
// r2.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = 7;
r2.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = 7;
r2.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = 7;
xlSheet.PrintPreview(xlSheet);//???
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
在问号的位置可以打印欲揽,可是我不知道PrintOut方法怎么用的啊?
...全文
78 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
chsfly 2004-11-12
  • 打赏
  • 举报
回复
PrintOut 方法使您能够打印指定的对象。您可以包括许多可选参数,其中包括打印范围(起始页和截止页)、份数、打印前是否预览、使用的打印机名称、是否需要打印到文件、是否需要逐份打印以及要打印到的文件的名称。

打印工作表
((Excel.Worksheet)ThisApplication.ActiveWorkbook.Sheets[1]).PrintOut(
1, 1, 2, true, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);

PrintPreview 方法允许您在“打印预览”窗口显示指定的对象。

打印前预览页面
((Excel.Worksheet)ThisApplication.ActiveWorkbook.Sheets[1]).
PrintPreview(Type.Missing);

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧