winform中想调用一个EXECL模版进行填充数据,再打印,怎么做? 各位,如果想调用一个EXECL模版进行填充数据,再打印,怎么做?需要什么组件?而且需要直接点打印,不显示excel界面,直接打印出这个EXCEL样式出来。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// /// </summary> /// <param name="sheetPrefixName">WorkSheet前缀名,比如:前缀名为“Sheet”,那么WorkSheet名称依次为“Sheet-1,Sheet-2”</param> public void Copy(string sheetPrefixName) { int sheetCount = 8; DateTime beforeTime; DateTime afterTime; if (sheetPrefixName == null || sheetPrefixName.Trim() == "" ) sheetPrefixName = " Sheet " ; // 创建一个Application对象并使其可见 beforeTime = DateTime.Now; Excel.Application app = new Excel.ApplicationClass(); app.Visible = true ; afterTime = DateTime.Now; // 打开模板文件,得到WorkBook对象 Excel.Workbook workBook = app.Workbooks.Open(templetFile,missing,missing,missing,missing,missing, missing,missing,missing,missing,missing,missing,missing,missing,missing); // 得到WorkSheet对象 Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Sheets.get_Item( 1 ); // 复制sheetCount-1个WorkSheet对象 for ( int i = 1 ;i < sheetCount;i ++ ) { ((Excel.Worksheet)workBook.Worksheets.get_Item(i)).Copy(missing,workBook.Worksheets[i]); } #region 将源DataTable数据写入Excel // for ( int i = 1 ;i <= sheetCount;i ++ ) // { // int startRow = (i - 1 ) * rows; // 记录起始行索引 // int endRow = i * rows; // 记录结束行索引 // // // 若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数 // if (i == sheetCount) // endRow = rowCount; // // // 获取要写入数据的WorkSheet对象,并重命名 // Excel.Worksheet sheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i); // sheet.Name = sheetPrefixName + " - " + i.ToString(); // // // 将dt中的数据写入WorkSheet // for ( int j = 0 ;j < endRow - startRow;j ++ ) // { // for ( int k = 0 ;k < colCount;k ++ ) // { // sheet.Cells[top + j,left + k] = dt.Rows[startRow + j][k].ToString(); // } // } // // // 写文本框数据 // Excel.TextBox txtAuthor = (Excel.TextBox)sheet.TextBoxes( " txtAuthor " ); // Excel.TextBox txtDate = (Excel.TextBox)sheet.TextBoxes( " txtDate " ); // Excel.TextBox txtVersion = (Excel.TextBox)sheet.TextBoxes( " txtVersion " ); // // txtAuthor.Text = " KLY.NET的Blog " ; // txtDate.Text = DateTime.Now.ToShortDateString(); // txtVersion.Text = " 1.0.0.0 " ; // } #endregion // 输出Excel文件并退出 try { workBook.SaveAs(outputFile,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing,missing); workBook.Close( null , null , null ); app.Workbooks.Close(); app.Application.Quit(); app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); workSheet = null ; workBook = null ; app = null ; GC.Collect(); } catch (Exception e) { throw e; } finally { Process[] myProcesses; DateTime startTime; myProcesses = Process.GetProcessesByName( " Excel " ); // 得不到Excel进程ID,暂时只能判断进程启动时间 foreach (Process myProcess in myProcesses) { startTime = myProcess.StartTime; if (startTime > beforeTime && startTime < afterTime) { myProcess.Kill(); } } } } 实际上,如果真的要做利用EXCEL显示。我觉得只要将数据显示在EXCEL中就可以,用户可以自己根据数据透视图等处理数据。利用EXCEL的功能来打印什么的。一般用户都可以接受。否则自己也非常麻烦。 Aspose控件达到LZ的要求。allisnew的专栏里好像有这个控件的PJ版。地址如下:http://blog.csdn.net/allisnew/default.aspx 对了,忘记说。示例可以到Aspose的官网上下。很简单的。 用这种方法Execl进程杀不掉,放在服务器端是个弊端。建议使用NOPI来操作。 Remoting服务端放在ADSL网络中,返回远程对象至客户端,调用时出问题 实现 正则表达式 匹配 操作超时,怎么解决呢,谢谢了 C# 时间 向前辈请教关于如何利用实体类之间关系对数据库进行操作的问题 有什么办法在界面上画线,vs2005 c#界面 如何实现对dataGrid中的一行数据进行修改 一個很难的问题,求助?大哥们!!! 想写一个软件,怎么根据操作系统语言的不同显示相应的版本? 关于C#写入WORD 怎样把多个安装程序打包成一个.msi文件,进行域的部署安装推广 ajax.net c#获取javascript的object问题,(顶也有分)
/// <summary>
///
/// </summary>
/// <param name="sheetPrefixName">WorkSheet前缀名,比如:前缀名为“Sheet”,那么WorkSheet名称依次为“Sheet-1,Sheet-2”</param>
public void Copy(string sheetPrefixName)
{
int sheetCount = 8;
DateTime beforeTime;
DateTime afterTime;
if (sheetPrefixName == null || sheetPrefixName.Trim() == "" )
sheetPrefixName = " Sheet " ;
// 创建一个Application对象并使其可见
beforeTime = DateTime.Now;
Excel.Application app = new Excel.ApplicationClass();
app.Visible = true ;
afterTime = DateTime.Now;
// 打开模板文件,得到WorkBook对象
Excel.Workbook workBook = app.Workbooks.Open(templetFile,missing,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing,missing,missing,missing);
// 得到WorkSheet对象
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Sheets.get_Item( 1 );
// 复制sheetCount-1个WorkSheet对象
for ( int i = 1 ;i < sheetCount;i ++ )
{
((Excel.Worksheet)workBook.Worksheets.get_Item(i)).Copy(missing,workBook.Worksheets[i]);
} #region 将源DataTable数据写入Excel
// for ( int i = 1 ;i <= sheetCount;i ++ )
// {
// int startRow = (i - 1 ) * rows; // 记录起始行索引
// int endRow = i * rows; // 记录结束行索引
//
// // 若是最后一个WorkSheet,那么记录结束行索引为源DataTable行数
// if (i == sheetCount)
// endRow = rowCount;
//
// // 获取要写入数据的WorkSheet对象,并重命名
// Excel.Worksheet sheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
// sheet.Name = sheetPrefixName + " - " + i.ToString();
//
// // 将dt中的数据写入WorkSheet
// for ( int j = 0 ;j < endRow - startRow;j ++ )
// {
// for ( int k = 0 ;k < colCount;k ++ )
// {
// sheet.Cells[top + j,left + k] = dt.Rows[startRow + j][k].ToString();
// }
// }
//
// // 写文本框数据
// Excel.TextBox txtAuthor = (Excel.TextBox)sheet.TextBoxes( " txtAuthor " );
// Excel.TextBox txtDate = (Excel.TextBox)sheet.TextBoxes( " txtDate " );
// Excel.TextBox txtVersion = (Excel.TextBox)sheet.TextBoxes( " txtVersion " );
//
// txtAuthor.Text = " KLY.NET的Blog " ;
// txtDate.Text = DateTime.Now.ToShortDateString();
// txtVersion.Text = " 1.0.0.0 " ;
// }
#endregion // 输出Excel文件并退出
try
{
workBook.SaveAs(outputFile,missing,missing,missing,missing,missing,Excel.XlSaveAsAccessMode.xlExclusive,missing,missing,missing,missing,missing);
workBook.Close( null , null , null );
app.Workbooks.Close();
app.Application.Quit();
app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app); workSheet = null ;
workBook = null ;
app = null ; GC.Collect();
}
catch (Exception e)
{
throw e;
}
finally
{
Process[] myProcesses;
DateTime startTime;
myProcesses = Process.GetProcessesByName( " Excel " ); // 得不到Excel进程ID,暂时只能判断进程启动时间
foreach (Process myProcess in myProcesses)
{
startTime = myProcess.StartTime; if (startTime > beforeTime && startTime < afterTime)
{
myProcess.Kill();
}
}
}
}
http://blog.csdn.net/allisnew/default.aspx