c++ builder2010中生成excel报表时,怎么设置列宽,文字居中?

baidu_20834545 2015-04-02 09:57:32
利用c++builder2010生成数据报表时,Sheet.OlePropertyGet("Columns", 7).OlePropertySet("ColumnWidth", 20);Sheet.OlePropertyGet("Cells").OlePropertySet("VerticalAlignment", 2); 这样设置列宽和文字居中格式 没有任何作用,是什么原因呢?
...全文
412 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_20834545 2015-04-09
  • 打赏
  • 举报
回复
多谢楼上的耐心解答,已经解决。。。
baidu_20834545 2015-04-07
  • 打赏
  • 举报
回复
class THH22RecordExportTool : public TForm
{
__published:	// IDE-managed Components
	TGroupBox *UART;
	TLabel *Comm;
	TLabel *Baudrate;
	TComboBox *CommCBox;
	TComboBox *BaudRateCBox;
	TButton *OpenBtn;
	TButton *CloseBtn;
	TButton *FindCommBtn;
	TMemo *Memo1;
	TEdit *Edit1;
	TGroupBox *GroupBox1;
	TLabel *RecordLbl1;
	TLabel *RecordLbl2;
	TEdit *RecordNum;
	TButton *GetInPKRecordBtn;
	TButton *GetAddTimeRecordBtn;
	TButton *GetOutPKRecordBtn;
	TLabel *RecordLbl3;
	TLabel *LblMid;
	TEdit *RecordNum1;
	TLabel *RecordLbl4;
	TPanel *Panel1;
	void __fastcall FindCommBtnClick(TObject *Sender);
	void __fastcall OpenBtnClick(TObject *Sender);
	void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
	void __fastcall CloseBtnClick(TObject *Sender);
	void __fastcall GetInPKRecordBtnClick(TObject *Sender);
	void __fastcall GetAddTimeRecordBtnClick(TObject *Sender);
	void __fastcall GetOutPKRecordBtnClick(TObject *Sender);
private:	// User declarations
	HANDLE g_hUartCommDll;
	bool  g_bUartOpen;
	unsigned long g_MachineNum;

	ST_CAR_INPARK_RECORD  stCarInParkRecord;
	ST_CAR_EXPARK_RECORD  stCarExParkRecord;
	ST_CARD_ADDTIME_RECORD  stCardAddTimeRecord;
	TURN_DATA tData;

	AnsiString appPath;
	AnsiString SavePath;
	AnsiString SaveAsPath;
	AnsiString xlsPath;
public:		// User declarations
	__fastcall THH22RecordExportTool(TComponent* Owner);
	void __fastcall AddLog(bool bAddTime, UnicodeString str);
	uchar8* _fastcall TimeNumToStr(uchar8* TimeBuf);
	void __fastcall THH22RecordExportTool::AddInParkRecordToExl(void);
	void __fastcall THH22RecordExportTool::AddADTimeRecordToExl(void);
	void __fastcall THH22RecordExportTool::AddOutParkRecordToExl(void);
};
void __fastcall THH22RecordExportTool::AddInParkRecordToExl(void)
{
	UnicodeString Ustr;
	AnsiString Astr;
	uchar8 strBuf[32] = {0};
	SavePath = appPath + "Record\\";
	SaveAsPath = appPath + "Record\\Files\\入场记录.xls";
	if(!DirectoryExists(SavePath))    //确保路径的有效性
	{
		CreateDir(SavePath);
	}
	xlsPath = SavePath +"入场记录" +Now().FormatString("yyyymmdd") + ".xls";

	if(!FileExists(xlsPath))
	{
		try
		{
			TStrings* lst = new TStringList();
			Astr = "记录笔数\t用户卡号\t操作员卡号\t用户卡有效性\t用户卡类\t用户姓名(车牌)\t入场时间\t有效期";
			lst->Add(Astr);
			lst->SaveToFile(xlsPath);
			delete lst;lst=0;
		}
   		catch(...){}
	}
	Variant Ex,Wb,Sheet;
	//建立Excel的Ole对象Ex
	try
	{
		CoInitialize(NULL);     // 解决下面运行出错的问题
		Ex = Variant::CreateObject("Excel.Application");
	}
	catch(...)
	{
		ShowMessage("无法启动Excel");
		return;
	}

 	try
	{
		Ex.OlePropertySet("Visible",true); //设置Excel为不可见
	 	Ex.OlePropertySet("DisplayAlerts",false); //关闭所有提示信息
		Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",xlsPath.c_str()); //打开指定的Excel报表文件。报表文件中最好设定只有一个Sheet。
		Wb = Ex.OlePropertyGet("ActiveWorkBook");
		Sheet = Wb.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet
		int iRows = 1;//记录行数
		int iCols = 1;//记录列数
		AnsiString s1;
		while(1)      //读取空行
		{
			s1 = Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertyGet("Value");
			if(s1.IsEmpty())
			{
				break;
			}
			else
			{
				iRows++ ; //继续检查下一行
			}
			Sleep(100);
		}
		//卡号、操作员卡号           RecordNum->Text     VerticalAlignment
	   	Sheet.OlePropertyGet("Cells").OlePropertySet("NumberFormatLocal", "@"); // 设置数据  以文本形式存储
		Sheet.OlePropertyGet("Cells").OlePropertySet("VerticalAlignment", 2); //设置文本居中对齐
		Sheet.OlePropertyGet("Columns", 7).OlePropertySet("ColumnWidth", 30);    //设置第7列列宽
		Sheet.OlePropertyGet("Cells", 1, 7).OlePropertySet("ColumnWidth", 30);
		Astr = RecordNum->Text;
		Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());
		Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",UIntToStr((uint32)stCarInParkRecord.u16CardNo).t_str());
		Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",UIntToStr((uint32)stCarInParkRecord.u16OptNo).t_str());
		//卡有效性
		if(0 == stCarInParkRecord.u8Valid)
		{
			Astr = "有效";
		}
		else
		{
			Astr = "无效";
		}
		Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());  //有效性
		//用户卡类
		if(0 == stCarInParkRecord.u8CardType)
		{
			 Astr = "临时卡";
		}
		else if(1 == stCarInParkRecord.u8CardType)
		{
			Astr = "月租卡";
		}
		Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());
		//用户姓名或车牌
		Astr.sprintf("%s", stCarInParkRecord.Identity);
		Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());
		//入场时间
		memcpy(strBuf, TimeNumToStr(stCarInParkRecord.InTime), 32);
		Astr.sprintf("%s", strBuf);
		Sheet.OlePropertyGet("Columns", 7).OlePropertySet("ColumnWidth", 20);    //设置第7列列宽
		Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());
		//有效期
		if(1 == stCarInParkRecord.u8CardType)
		{
			Astr.printf("20%02d-%02d-%02d",stCarInParkRecord.ValidityData[0],
						stCarInParkRecord.ValidityData[1], stCarInParkRecord.ValidityData[2]);
			Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());
		}

	 //	Wb.OleProcedure("Save");//保存表格
	 //	Sheet=Unassigned;
	//	Wb.OleProcedure("Close");//关闭表格

		Wb.OleFunction("SaveAs", SaveAsPath.c_str());
	//	Ex.OleFunction("Quit");//退出Excel

		//记录数加1,准备导出下一笔记录
		Ustr = "SLV->导出第" + RecordNum->Text +"笔记录成功!";
		int tNum = StrToInt(RecordNum->Text);
		RecordNum->Text = IntToStr(++tNum);
		AddLog(0, Ustr);

	}
    catch(...)
	{
		Application->MessageBox(L"Excel操作出错!", L"错误",MB_ICONSTOP|MB_OK);
	}

}
baidu_20834545 2015-04-07
  • 打赏
  • 举报
回复
对哦 你说的确实可以啊!但是只能看到效果啊,如何把这效果的形式也保存起来呢? 也就是当别人打开excel文件的时候,格式就和这效果的一样,不用手动去设置。我用Wb.OleFunction("SaveAs", SaveAsPath.c_str());来另存,可是出现这样的错误
我发这个源码贴出了,麻烦你看看怎么修改啊?
宝龙哥 2015-04-03
  • 打赏
  • 举报
回复
大兄弟啊!人家说的对啊 你把 Ex.OlePropertySet("Visible",false); //设置Excel为不可见 改为Ex.OlePropertySet("Visible",true); //设置Excel为不可见 然后把后面退出Excel的代码去掉,让Excel窗体显示在屏幕上,你就能看到效果了
宝龙哥 2015-04-03
  • 打赏
  • 举报
回复
我用XE7测试你的代码,好用
宝龙哥 2015-04-03
  • 打赏
  • 举报
回复
你的这个代码我实测好用,至于原因,想来,BCB出现这种问题一点都不奇怪 你新建一个工程,把你的代码放进去试试看什么结果
宝龙哥 2015-04-02
  • 打赏
  • 举报
回复
引用 3 楼 baidu_20834545 的回复:
我设置的应该是跟你的方法一样啊。没有作用。 还有其他方面的原因吗
你设置的真的和我的一样吗?你确定么?
baidu_20834545 2015-04-02
  • 打赏
  • 举报
回复
我设置的应该是跟你的方法一样啊。没有作用。 还有其他方面的原因吗
宝龙哥 2015-04-02
  • 打赏
  • 举报
回复
Sheet.OlePropertyGet("Columns").OlePropertySet("ColumnWidth",28); // 设置所有列的列宽为28
Sheet.OlePropertyGet("Columns",3).OlePropertySet("ColumnWidth",68); // 设置第3列的列宽为28

Sheet.OlePropertyGet("Cells",1,1).OlePropertySet("HorizontalAlignment",3); // 设置指定单元格文字居中
baidu_20834545 2015-04-02
  • 打赏
  • 举报
回复
.cpp文件中生成报表对象的相关相关代码如下:
try
{
CoInitialize(NULL); // 解决下面运行出错的问题
Ex = Variant::CreateObject("Excel.Application");
}
catch(...)
{
ShowMessage("无法启动Excel");
return;
}
try
{
Ex.OlePropertySet("Visible",false); //设置Excel为不可见
Ex.OlePropertySet("DisplayAlerts",false); //关闭所有提示信息
Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",xlsPath.c_str()); //打开指定的Excel报表文件。报表文件中最好设定只有一个Sheet。
Wb = Ex.OlePropertyGet("ActiveWorkBook");
Sheet = Wb.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet

.h中的头文件如下:
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>

#include <ComCtrls.hpp>
#include <CheckLst.hpp>
#include <Dialogs.hpp>
#include <IniFiles.hpp>
#include <FileCtrl.hpp>
#include <Comobj.hpp>
#include <OleServer.hpp>

excel单元格式无法设置成功,是因为缺少什么头文件还是哪的原因啊?求解
baidu_20834545 2015-04-02
  • 打赏
  • 举报
回复
我把try 都去掉了。可以正常运行啊,没有别的错误信息。我查网上,别人说是生成了SCV格式的报表,并没有生成真正的.xls格式的报表?这又是怎么回事呢? 这是根据网上别人写的生成excel报表的方法写的,有的代码里面就有设置行列的。
宝龙哥 2015-04-02
  • 打赏
  • 举报
回复
手边没有环境不能测试,不过初步怀疑是前面的语句出错退出了,而 try 使错误信息没有显示出来,你把try去掉看有没有出错信息
baidu_20834545 2015-04-02
  • 打赏
  • 举报
回复
.cpp相关内容
	if(!FileExists(xlsPath))
	{
		try
		{
			TStrings* lst = new TStringList();
			Astr = "记录笔数\t用户卡号\t操作员卡号\t用户卡有效性\t用户卡类\t用户姓名(车牌)\t入场时间\t有效期";
			lst->Add(Astr);
			lst->SaveToFile(xlsPath);
			delete lst;lst=0;
		}catch(...){}
	}
	Variant Ex,Wb,Sheet;
	//建立Excel的Ole对象Ex
	try
	{
		CoInitialize(NULL);     // 解决下面运行出错的问题
		Ex = Variant::CreateObject("Excel.Application");
	}
	catch(...)
	{
		ShowMessage("无法启动Excel");
		return;
	}
	try
	{
		Ex.OlePropertySet("Visible",false); //设置Excel为不可见
		Ex.OlePropertySet("DisplayAlerts",false); //关闭所有提示信息
		Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",xlsPath.c_str()); //打开指定的Excel报表文件。报表文件中最好设定只有一个Sheet。
		Wb = Ex.OlePropertyGet("ActiveWorkBook");
		Sheet = Wb.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet
		int iRows = 1;//记录行数
		int iCols = 1;//记录列数
		AnsiString s1;
		while(1)      //读取空行
		{
			s1 = Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertyGet("Value");
			if(s1.IsEmpty())
			{
				break;
			}
			else
			{
				iRows++ ; //继续检查下一行
			}
			Sleep(100);
		}
		//卡号、操作员卡号           RecordNum->Text     VerticalAlignment
		Sheet.OlePropertyGet("Columns", 7).OlePropertySet("ColumnWidth", 30);    //设置第7列列宽
		Sheet.OlePropertyGet("Cells").OlePropertySet("ColumnWidth", 30);
.h 相关头文件
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>

#include <ComCtrls.hpp>
#include <CheckLst.hpp>
#include <Dialogs.hpp>
#include <IniFiles.hpp>
#include <FileCtrl.hpp>
#include <Comobj.hpp>
#include <OleServer.hpp>
是少包含什么头文件吗?头大啊
宝龙哥 2015-04-02
  • 打赏
  • 举报
回复
把完整代码贴出来帮你看看
baidu_20834545 2015-04-02
  • 打赏
  • 举报
回复
	Sheet.OlePropertyGet("Columns", 7).OlePropertySet("ColumnWidth", 20);    //设置第7列列宽
这是代码,怎么回事不行

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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