C#操作excel打印的示例
usingSystem;
usingSystem.Data;
usingSystem.IO;
usingSystem.Runtime.InteropServices;
usingSystem.Threading;
usingSystem.Windows.Forms;
usingMicrosoft.Office.Core;
usingExcel=Microsoft.Office.Interop.Excel;
namespaceWindowsFormsApplication1
{
publicpartialclassForm1:Form
{
privateboolisStartPrint=false;
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
//stringOriginalPath=Path.GetDirectoryName(Application.ExecutablePath)+"\\IMS.accdb";
////初始化数据库
//DataBase.OleDbObject.InitDatabase(OriginalPath);
//DataBase.OleDbObject.OpenDb();
//intk=DataBase.OleDbObject.Select("select*fromElementInventory").Rows.Count;
//初始化要写入Excel的数据
DataTabledt=newDataTable();
for(inti=0;i<11;i++)
{
DataColumndc=newDataColumn();
dc.ColumnName="name"+i;
dc.DataType=typeof(string);
dt.Columns.Add(dc);
}
for(inti=0;i<30;i++)
{
DataRowdr=dt.NewRow();
if(i%2==0)
{
dr[0]="测试用例"+i+"号,开始测试是否自动换行,以及页面高度自适应。";
}
else
{
dr[0]="测试用例"+i+"号";
}
for(intj=1;j<11;j++)
{
dr[j]=j;
}
dt.Rows.Add(dr);
}
if(!isStartPrint)
{
Threadth=newThread(delegate()
{
DataTabletoExcel(dt,"测试.xlsx");
});
th.Start();
isStartPrint=true;
}
else
{
MessageBox.Show("打印程序已启用,请稍后……","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
//DataTabletoExcel(dt,"测试.xlsx");
}
publicvoidDataTabletoExcel(System.Data.DataTabledtTemp,stringstrFileName)
{
introwNum=dtTemp.Rows.Count;//先得到dtTemp的行数
intcolumnNum=dtTemp.Columns.Count;//列数
Excel.ApplicationExcelApp=newExcel.Application();//声明一个应用程序类实例
//ExcelApp.DefaultFilePath="";//默认文件路径导出excel的路径还是在参数strFileName里设置
//ExcelApp.DisplayAlerts=true;
//ExcelApp.SheetsInNewWorkbook=1;///返回或设置MicrosoftExcel自动插入到新工作簿中的工作表数目。
Excel.WorkbookworksBook=ExcelApp.Workbooks.Add();//创建一个新工作簿
Excel.WorksheetworkSheet=(Excel.Worksheet)worksBook.Worksheets[1];//在工作簿中得到sheet。
if(workSheet==null)
{
System.Diagnostics.Debug.WriteLine("ERROR:worksheet==null");
return;
}
workSheet.Name="测试Sheet1";//工作表的名称
workSheet.Cells.WrapText=true;//设置所有列的文本自动换行
workSheet.Cells.EntireRow.AutoFit();//设置所有列自动调整行高
#region绘制列
///自定义方法,向sheet中绘制列
RangeMark(workSheet,"A1","A2","合并竖列1");
RangeMark(workSheet,"B1","B2","合并竖列2");
RangeMark(workSheet,"C1","C2","合并竖列3");
RangeMark(workSheet,"D1","D2","合并竖列4");
RangeMark(workSheet,"E1","E2","合并竖列5");
RangeMark(workSheet,"F1","H1","合并横行1");
RangeMark(workSheet,"F2","F2","合并横行1.1");
RangeMark(workSheet,"G2","G2","合并横行1.2");
RangeMark(workSheet,"H2","H2","合并横行1.3");
RangeMark(workSheet,"I1","K1","合并横行2");
RangeMark(workSheet,"I2","J2","合并横行2.1");
RangeMark(workSheet,"K2","K2","合并横行2.2");
#endregion
//将DataTable中的数据导入Excel中
for(inti=0;i
///引用Windows句柄,获取程序PID
///
///
///
///
[DllImport("User32.dll")]
publicstaticexternintGetWindowThreadProcessId(IntPtrHwnd,outintPID);
///
///杀掉生成的进程
///
///进程程对象
privatestaticvoidKillProcess(Excel.ApplicationAppObject)
{
intPid=0;
IntPtrHwnd=newIntPtr(AppObject.Hwnd);
System.Diagnostics.Processp=null;
try
{
GetWindowThreadProcessId(Hwnd,outPid);
p=System.Diagnostics.Process.GetProcessById(Pid);
if(p!=null)
{
p.Kill();
p.Dispose();
}
}
catch(Exceptionex)
{
System.Diagnostics.Debug.WriteLine("进程关闭失败!异常信息:"+ex);
}
}
///
///创建表头单元格,包括合并单元格
///
///工作表
///单元格起始格编号
///单元格结束编号
///单元格名称
privatestaticboolRangeMark(Excel.WorksheetworkSheet,stringstartCell,stringendCell,stringstrText)
{
//创建一个区域对象。第一个参数是开始格子号,第二个参数是终止格子号。比如选中A1——D3这个区域。
Excel.Rangerange=(Excel.Range)workSheet.get_Range(startCell,endCell);
if(range==null)
{
System.Diagnostics.Debug.WriteLine("ERROR:range==null");
returnfalse;
}
range.Merge(0);//合并方法,0的时候直接合并为一个单元格
range.Font.Size=16;//字体大小
range.Font.Name="黑体";//字体
range.WrapText=true;//文本自动换行
range.EntireRow.AutoFit();//自动调整行高
//range.RowHeight=20;
//range.EntireColumn.AutoFit();//自动调整列宽
range.ColumnWidth=15;
range.HorizontalAlignment=XlVAlign.xlVAlignCenter;//横向居中
range.Value=strText;//合并单元格之后,设置其中的文本
range.Interior.ColorIndex=20;//填充颜色
range.Cells.Borders.LineStyle=1;//设置单元格边框的粗细
returntrue;
}
}
}
以上就是C#操作excel打印的示例的详细内容,更多关于c#打印的资料请关注毛票票其它相关文章!