欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

excel 设置单元格对齐方式

程序员文章站 2024-02-24 13:35:34
...
   enum CellHorizonAlignment
	{
		xlCenter = -4108,
		xlLeft   = -4131,
		xlRight  = -4152,
		xlFill   = 5,
		xlJustify= -4130,
		xlCenterAcrossSelection = 7,
		xlDistributed= -4117
	};
	enum CellVerticalAlignment
	{
       xlTop = -4160,
	   xlBottom = -4107,
	   xlvJustify= -4130,
	   xlvDistributed= -4117
	};


//设置单元格对齐方式
void COperExcel::XlsSetCellAlignment(int row, int colum, CellHorizonAlignment horizon, CellVerticalAlignment vertical)
{
	if (!xlsAppIsInit())
	{
		return;
	}
	/*
	row  colum  判断
	*/
	if (!(row >= 1 && colum >= 1))
	{
		;
		return;
	}

	CWorksheet xlsWorkSheet = m_xlsAppLication.get_ActiveSheet();
	if (xlsWorkSheet.m_lpDispatch == NULL)
	{
		return;
	}
	CRange range;
	LPDISPATCH lpDisp = NULL;
	lpDisp = xlsWorkSheet.get_Cells();  //所有表格
	if (lpDisp == NULL)
	{
		return;
	}

	range.AttachDispatch(lpDisp);
	if (range.m_lpDispatch == NULL)
	{
		return;
	}
	
	range.get_Item(_variant_t((long)row), _variant_t((long)colum));
	switch(horizon)
	{
	case COperExcel::xlLeft:
		range.put_HorizontalAlignment(_variant_t(xlLeft));
		break;
	case COperExcel::xlRight:
		range.put_HorizontalAlignment(_variant_t(xlRight));
		break;
	case COperExcel::xlCenter:
		range.put_HorizontalAlignment(_variant_t(xlCenter));
		break;
	case COperExcel::xlFill:
		range.put_HorizontalAlignment(_variant_t(xlFill));
		break;
	case COperExcel::xlJustify:
		range.put_HorizontalAlignment(_variant_t(xlJustify));
		break;
	case COperExcel::xlCenterAcrossSelection:
		range.put_HorizontalAlignment(_variant_t(xlCenterAcrossSelection));
		break;
	case COperExcel::xlDistributed:
		range.put_HorizontalAlignment(_variant_t(xlDistributed));
		break;
	default:
		break;
	}
	switch (vertical)
	{
	case COperExcel::xlTop:
		range.put_VerticalAlignment(_variant_t(xlTop));
		break;
	case COperExcel::xlBottom:
		range.put_VerticalAlignment(_variant_t(xlBottom));
		break;
	case COperExcel::xlJustify:
		range.put_VerticalAlignment(_variant_t(xlJustify));
		break;
	case COperExcel::xlDistributed:
		range.put_VerticalAlignment(_variant_t(xlDistributed));
		break;
	default:
		break;
	}
	range.ReleaseDispatch();
	xlsWorkSheet.ReleaseDispatch();
	lpDisp = NULL;
}