C++Builder中怎样将ListView列表类容保存为EXCLE或者txt格式?又如何导入EXCLE保存的文件

qingtianlingzi 2011-01-06 08:32:52
各个大侠,

网上找了很多这方面资料,但是没找到适合c++builder开发的,书上没找到这方面相关滴资料,求各位大侠解答,

需要将ListView列表类容保存为EXCLE或者txt格式?即导出,如果需要导入之前保存的excle或者txt文件,又如何导入到

listview中呢。。。。。。。????????????
...全文
521 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
下下弦月 2012-02-20
  • 打赏
  • 举报
回复
这个是老妖ccrun的代码修改后自己弄的函数,但是导出的速度暂时不是很理想
//导出函数,传入listview类型的指针和保存路径,则自动保存
void __fastcall TShare::dc_excel(TListView *lv, AnsiString SaveTo)
{
// Variant vExcelApp, vSheet;
// try
// {
// vExcelApp = Variant::CreateObject("Excel.Application");
// }
// catch(...)
// {
// MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
// "ListView2Excel", MB_OK | MB_ICONERROR);
// vExcelApp = Unassigned;
// return;
// }
// // 隐藏Excel界面
// vExcelApp.OlePropertySet("Visible", false);
// // 新建一个工作表
// vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表
// // 操作这个工作表
// vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook")
// .OlePropertyGet("Sheets", 1);
// // 设置Excel文档的字体
// vSheet.OleProcedure("Select");
// vSheet.OlePropertyGet("Cells").OleProcedure("Select");
// vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
// .OlePropertySet("Size", lv->Font->Size);
// vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
// .OlePropertySet("Name", lv->Font->Name.c_str());
// vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
// .OlePropertySet("FontStyle", "常规");
// vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select");
// // 表格的行数
// int nRowCount(lv->Items->Count + 1);
// nRowCount = nRowCount < 2? 2: nRowCount;
// // 表格的列数
// int nColCount(lv->Columns->Count);
// nColCount = nColCount < 1? 1: nColCount;
// // 设置单元格的宽度
// for(int i=0; i<nColCount; i++)
// {
// int nColWidth;
// if(lv->Columns->Count > i)
// nColWidth = lv->Columns->Items[i]->Width;
// else
// nColWidth = 100; // 如果没有设置列,就随便设置个默认值
// vExcelApp.OlePropertyGet("Columns", i + 1)
// .OlePropertySet("ColumnWidth", nColWidth/10 );
// }
// //设置单元格对齐
// for(int i=0; i<nColCount; i++)
// for(int j=0; j<nRowCount; j++)
// {
// //垂直方向对齐
// // 1=靠上 2=居中 3=靠下对齐 4=两端对齐 5=分散对齐
// vExcelApp.OlePropertyGet("Cells", j+1 ,i+1).OlePropertySet("VerticalAlignment", 2);
// //水平方向对齐
// // 1=靠上 2=居中 3=靠下对齐 4=两端对齐 5=分散对齐
// vExcelApp.OlePropertyGet("Cells", j+1, i+1).OlePropertySet("HorizontalAlignment", 3);
// }
//
// //设置Excel不自动刷新
// vExcelApp.OlePropertySet("ScreenUpdating",false);
// // 先将列名写入Excel表格
// for(int j=0; j<lv->Columns->Count; j++)
// {
// // 标题行的行高
// vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20);
// // 写入标题
// vSheet.OlePropertyGet("Cells", 1, j + 1)
// .OlePropertySet("Value",
// lv->Columns->Items[j]->Caption.c_str());
// // 设置列名单元格的背景色
// Variant vInter = vSheet.OlePropertyGet(
// "Cells", 1, j + 1).OlePropertyGet("Interior");
// vInter.OlePropertySet("ColorIndex", 15); // 灰色
// vInter.OlePropertySet("Pattern", 1); // xlSolid
// vInter.OlePropertySet("PatternColorIndex", -4105); // xlAutomatic
// }
// // 将ListView中的数据写入Excel表格
// AnsiString Value;
// for(int i=0; i<nRowCount - 1; i++)
// {
//
// // 普通数据行的行高16
// vExcelApp.OlePropertyGet("Rows", i + 2).OlePropertySet("RowHeight", 16);
// Value = "'" + lv->Items->Item[i]->Caption;
// vSheet.OlePropertyGet("Cells", i + 2, 1)
// .OlePropertySet("Value", Value.c_str());
// for(int j = 0; j<lv->Items->Item[i]->SubItems->Count; j++)
// {
// Value = "'" + lv->Items->Item[i]->SubItems->Strings[j];
// vSheet.OlePropertyGet("Cells", i + 2, j + 2)
// .OlePropertySet("Value", Value.c_str());
// }
// }
// //设置Excel自动刷新
// vExcelApp.OlePropertySet("ScreenUpdating",true);
// // 保存Excel文档并退出
// vExcelApp.OlePropertyGet("ActiveWorkbook")
// .OleFunction("SaveAs", SaveTo.c_str());
// vExcelApp.OleFunction("Quit");
// vSheet = Unassigned;
// vExcelApp = Unassigned;
// // 工作结束
// MessageBox(0, "文件导出成功",
// "Excel", MB_OK | MB_ICONINFORMATION);

BCBPLC 2011-01-12
  • 打赏
  • 举报
回复
用程序产生.txt格式,但文件名仍用.xls或.doc

如果是xls,生成的文本列与列之间用tab('\9)分隔;

简单快速.
ccrun.com 2011-01-07
  • 打赏
  • 举报
回复
将ListView中的内容导出到Word和Excel
http://www.ccrun.com/article.asp?i=633&d=vytvc6
kawb1974 2011-01-06
  • 打赏
  • 举报
回复
TXT格式 我刚才回答你了
EXCLE格式 加入控件
ExcelApplication1
ExcelWorkbook1
ExcelWorksheet1
然后用
Variant tg1,tg2,tg3;
tg1=1; //第1列
tg2=1; //第1行
tg3="数据";
ExcelWorksheet1->Cells->set_Item(tg1,tg2,tg3);
的格式写入即可

导入么反一反
用get_Item即可

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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