用excel打印报表
用代码设置excel单元格边框
eSheet.Range("C" & sPoint).Value = r("STNM") '站名
其他:
//变量定义
_Application app;
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
Range range;
Range iCell;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//*****
//初始化COM的动态连接库
if(!AfxOleInit())
{
AfxMessageBox("无法初始化COM的动态连接库!");
return ;
}
//*****
//创建Excel 2000服务器(启动Excel)
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("无法启动Excel服务器!");
return;
}
app.SetVisible(TRUE); //使Excel可见
app.SetUserControl(TRUE); //允许其它用户控制Excel
//*****
//打开c:\\1.xls
books.AttachDispatch(app.GetWorkbooks());
lpDisp = books.Open("C:\\\\1.xls",
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional );
//*****
//得到Workbook
book.AttachDispatch(lpDisp);
//*****
//得到Worksheets
sheets.AttachDispatch(book.GetWorksheets());
//*****
//得到当前活跃sheet
//如果有单元格正处于编辑状态中,此操作不能返回,会一直等待
lpDisp=book.GetActiveSheet();
sheet.AttachDispatch(lpDisp);
//*****
//读取已经使用区域的信息,包括已经使用的行数、列数、起始行、起始列
Range usedRange;
usedRange.AttachDispatch(sheet.GetUsedRange());
range.AttachDispatch(usedRange.GetRows());
long iRowNum=range.GetCount(); //已经使用的行数
range.AttachDispatch(usedRange.GetColumns());
long iColNum=range.GetCount(); //已经使用的列数
long iStartRow=usedRange.GetRow(); //已使用区域的起始行,从1开始
long iStartCol=usedRange.GetColumn(); //已使用区域的起始列,从1开始
//*****
//读取第一个单元格的值
range.AttachDispatch(sheet.GetCells());
range.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal );
COleVariant vResult =range.GetValue();
CString str;
if(vResult.vt == VT_BSTR) //字符串
{
str=vResult.bstrVal;
}
else if (vResult.vt==VT_R8) //8字节的数字
{
str.Format("%f",vResult.dblVal);
}
else if(vResult.vt==VT_DATE) //时间格式
{
SYSTEMTIME st;
VariantTimeToSystemTime(&vResult.date, &st);
}
else if(vResult.vt==VT_EMPTY) //单元格空的
{
str="";
}
//*****
//读取第一个单元格的对齐方式,数据类型:VT_I4
//读取水平对齐方式
range.AttachDispatch(sheet.GetCells());
iCell.AttachDispatch((range.GetItem (COleVariant(long(1)), COleVariant(long(1)))).pdispVal);
vResult.lVal=0;
vResult=iCell.GetHorizontalAlignment();
if(vResult.lVal!=0)
{
switch (vResult.lVal)
{
case 1: //默认
break;
case -4108: //居中
break;
case -4131 : //靠左
break;
case -4152 : //靠右
break;
}
}
//垂直对齐方式
iCell.AttachDispatch((range.GetItem (COleVariant(long(1)), COleVariant(long(1)))).pdispVal);
vResult.lVal=0;
vResult=iCell.GetVerticalAlignment();
if(vResult.lVal!=0)
{
switch (vResult.lVal)
{
case -4160 : //靠上
break;
case -4108 : //居中
break;
case -4107 : //靠下
break;
}
}
//*****
//设置第一个单元格的值"HI,EXCEL!"
range.SetItem(COleVariant(1),COleVariant(1),COleVariant("HI,EXCEL!"));
//*****
//设置第一个单元格字体颜色:红色
Font font;
range.AttachDispatch(sheet.GetCells());
range.AttachDispatch((range.GetItem (COleVariant(long(1)), COleVariant(long(1)))).pdispVal);
font.SetColor(COleVariant((long)0xFF0000));
//*****
//合并单元格的处理
//包括判断第一个单元格是否为合并单元格,以及将第一个单元格进行合并
Range unionRange;
range.AttachDispatch(sheet.GetCells());
unionRange.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal );
vResult=unionRange.GetMergeCells();
if(vResult.boolVal==-1) //是合并的单元格
{
//合并单元格的行数
range.AttachDispatch (unionRange.GetRows ());
long iUnionRowNum=range.GetCount ();
//合并单元格的列数
range.AttachDispatch (unionRange.GetColumns ());
long iUnionColumnNum=range.GetCount ();
//合并区域的起始行,列
long iUnionStartRow=unionRange.GetRow(); //起始行,从1开始
long iUnionStartCol=unionRange.GetColumn(); //起始列,从1开始
}
else if(vResult.boolVal==0)
{//不是合并的单元格}
//将第一个单元格合并成2行,3列
range.AttachDispatch(sheet.GetCells());
unionRange.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal );
unionRange.AttachDispatch(unionRange.GetResize(COleVariant((long)2),COleVariant((long)3)));
unionRange.Merge(COleVariant((long)0)); //合并单元格
//*****
//将文件保存为2.xls
book.SaveAs(COleVariant("C:\\\\2.xls"),covOptional,covOptional, \\
covOptional,covOptional,covOptional,0,\\
covOptional,covOptional,covOptional,covOptional);
//*****
//关闭所有的book,退出Excel
book.Close (covOptional,COleVariant(OutFilename),covOptional);
books.Close();
app.Quit();
保存:
Friend Sub Print(ByVal save As Boolean, ByVal filePath As String)
Dim filePate As String = Application.StartupPath & "\Report\报表模板.xls"
Dim appExcel As Excel.Application = New Excel.Application
Dim ebook As Excel.Workbook
Dim eSheet As Excel.Worksheet
Dim sPoint As Int32 = 3
Dim pointStr As String = ""
Dim unbPoint As Int32 = 0
Dim annex As Boolean = False
ebook = appExcel.Application.Workbooks.Open(filePate)
eSheet = ebook.Worksheets("特殊水情")
'写入标题
eSheet.Range("A1:E1").Value = Me.captionText
For Each r As DataRowView In CType(Me.dgList.DataSource, DataView).Table.DefaultView
If pointStr = r("TPCD") Then
unbPoint += 1
annex = True
Else
annex = False
eSheet.Range("A" & sPoint).BorderAround(1, 2, Excel.XlColorIndex.xlColorIndexAutomatic, 1)
eSheet.Range("A" & sPoint).Value = r("TPCDText") '信息类别
End If
eSheet.Range("B" & sPoint).BorderAround(1, 2, Excel.XlColorIndex.xlColorIndexAutomatic, 1)
eSheet.Range("B" & sPoint).Value = r("STCD") '站号
eSheet.Range("C" & sPoint).BorderAround(1, 2, Excel.XlColorIndex.xlColorIndexAutomatic, 1)
eSheet.Range("C" & sPoint).Value = r("STNM") '站名
eSheet.Range("D" & sPoint).BorderAround(1, 2, Excel.XlColorIndex.xlColorIndexAutomatic, 1)
eSheet.Range("D" & sPoint).Value = r("TM") '时间
eSheet.Range("E" & sPoint).BorderAround(1, 2, Excel.XlColorIndex.xlColorIndexAutomatic, 1)
eSheet.Range("E" & sPoint).Value = r("CONTENT") '信息内容
pointStr = r("TPCD")
If annex Then
'合并单元格
If unbPoint > 0 Then
eSheet.Range("A" & sPoint - unbPoint & ":A" & sPoint).BorderAround(1, 2, Excel.XlColorIndex.xlColorIndexAutomatic, 1)
eSheet.Range("A" & sPoint - unbPoint & ":A" & sPoint).Merge()
eSheet.Range("A" & sPoint - unbPoint & ":A" & sPoint).Value = r("TPCDText") '信息类别
unbPoint = 0
End If
End If
sPoint += 1
Next
If save Then
appExcel.Visible = False
Try
eSheet.SaveAs(filePath)
MessageBox.Show("保存完成! ", "保存文件", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("没有保存文件! ", "保存失败", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
Else
Try
appExcel.Visible = True
eSheet.PrintPreview()
Catch ex As Exception
MessageBox.Show("无法完成打印:" & ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
ebook.Close(False)
appExcel.Quit()
ebook = Nothing
appExcel = Nothing
End Sub
eSheet.Range("A1:" & eColumn & "1").Merge() '合并
eSheet.Range("A1:" & eColumn & "1").RowHeight = 25 '行高
eSheet.Range("A1:" & eColumn & "1").Font.Name = "隶书" '字体
eSheet.Range("A1:" & eColumn & "1").Font.Size = 24 '字号
eSheet.Range("A1:" & eColumn & "1").HorizontalAlignment = -4108 '水平居中
eSheet.Range("A1:" & eColumn & "1").Value = Me.dgList.CaptionText '文字
eSheet.Range("A1:" & eColumn & "1").BorderAround(1, 2, Excel.XlColorIndex.xlColorIndexAutomatic, 1) '边框
彻底关掉excel进程:
Marshal.ReleaseComObject(m_comObject)所有对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet1)
xlSheet1=Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject (wb)
wb=Nothing
System.Runtime.InteropServices.Marshal.ReleaseComObject(Excl)
Excl = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
Sub A()
Dim xlApp As New Excel.Application '创建一个excel应用程序对象
Dim xlBooks As Excel.Workbooks '声明一个workbooks对象
Dim xlbook As Excel.Workbook '声明一个xlbook对象
Dim xlSheets As Excel.Sheets
xlApp.Quit()
End Sub
Sub B()
A();
GC.Collect()
End Sub
GC不会回收他所在的区,所以用另一个方法调用你的实际后回收