一、如何用vc打开一个excel文件
//创建数据库,即一个Excel的XLS文件 ConnStr = "Driver={Microsoft Excel Driver (*.xls)};DBQ="+FileName+";"; ADOConnection->ConnectionString = WideString(ConnStr); ADOConnection->Open(); // //创建表,创建成功后再Xls文件中就能看见...
二、vc之ADO操作Excel
很早以前用过VC,现在用C#了。不过记得VC当初用的也是自动化对象,只要取得EXCEL的单元,想怎么设计表头,还不是自己写的事。
以下代码供您参考:
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Workbook excelWB = excelApp.Workbooks.Add(System.Type.Missing); //创建工作簿(WorkBook:即Excel文件主体本身)
Worksheet excelWS = (Worksheet)excelWB.Worksheets[1]; //创建工作表(即Excel里的子表sheet) 1表示在子表sheet1里进行数据导出
int nRange = 0;//最大范围
int nStartRow = 0;
foreach(DataGrid dg in dataGrids)
{
if (dg.Columns.Count() > nRange)
{
nRange = dg.Columns.Count();
}
}
char cMax = (char)((int)'A' + nRange);
if (nRange > 25)
{
nRange = 25;
cMax = 'Z';
}
// 如果数据中存在数字类型 可以让它变文本格式显示
//将数据导入到工作表的单元格
//写头:
if (strHeadList != null)
{
nStartRow = strHeadList.Count();
for (int i = 0; i < nStartRow; i++)
{
//int nHeight = strHeadList[0].Split('n').Count();
Range range = (Range)excelWS.get_Range("A" + (i + 1).ToString(), cMax.ToString() + (i + 1).ToString()); //获取Excel多个单元格区域:本例做为Excel表头
range.Merge(0); //单元格合并动作 要配合上面的get_Range()进行设计
//range.RowHeight = 100;
range.Borders.LineStyle = 1; //设置单元格边框的粗细
//range.Cells.Interior.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb(); //设置单元格的背景色
range.WrapText=true; //文本自动换行
int count = Regex.Matches(strHeadList[i], "n").Count;
range.RowHeight = (count+1) * 15;
//range.EntireRow.AutoFit();
range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThick, XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.FromArgb(255, 204, 153).ToArgb()); //给单元格加边框
//range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //设置单元格上边框为无边框
//range.Interior.ColorIndex = 39; //填充颜色为淡紫色
//range.Font.Color = System.Drawing.Color.FromArgb(255, 204, 153).ToArgb(); //字体颜色
//range.EntireColumn.AutoFit(); //自动调整列宽
//range.HorizontalAlignment=XlHAlign.xlHAlignCenter; ; // 文本水平居中方式
//range.VerticalAlignment = VerticalAlignment.Center; //文本垂直居中方式
if (bIsTitle)
{
range.Font.Bold = true;
range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
range.get_Offset(0, 0).Cells.Value = strHeadList[i];
}
else
range.get_Offset(0, 0).Value = strHeadList[i];
}
}
三、如何调用excel的API
这是我以前写的一段程序,可以参考。 现在没有安装Office,没法试了。
你在百度文库里找“使用MFC操作EXCEL文件”就可以找到资料。
_Application app; // app is an _Application object.
Workbooks books;
LPDISPATCH lpDisp; // Often reused variable.
if(!app.CreateDispatch("Excel.Application"))
{
AfxMessageBox("Couldn't CreateDispatch() for Excel");
return;
}
// Set visible.
app.SetVisible( FALSE );
lpDisp = app.GetWorkbooks(); // Get an IDispatch pointer.
ASSERT(lpDisp);
books.AttachDispatch(lpDisp); // Attach the IDispatch pointer
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// To open an existing workbook, you need to provide all
// 13 arguments for the Open member function.
// The code below opens a workbook and adds it to the Workbook's
// Collection object.
// You need to modify the path and file name for your own
// workbook.
//
CString strFile = filename;
lpDisp = books.Open( strFile, // is a workbook.
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional ); // Return Workbook's IDispatch pointer.
ASSERT(lpDisp);
book.AttachDispatch( lpDisp );
// Get the Sheets collection and attach the IDispatch pointer to your
// sheets object.
lpDisp = book.GetSheets();
ASSERT(lpDisp);
sheets.AttachDispatch(lpDisp);
long nSheets = sheets.GetCount();
char outfile[256];
for (long no = 1; no <= nSheets; no++)
{
lpDisp = sheets.GetItem( COleVariant((short)( no )) );
ASSERT(lpDisp);
sheet.AttachDispatch(lpDisp);
lpDisp = sheet.GetUsedRange();
ASSERT(lpDisp);
range.AttachDispatch(lpDisp);
/*
Range r = range.GetColumns();
int cc = r.GetCount();
r = range.GetRows();
int rr = r.GetCount();
*/
VARIANT ret = range.GetValue();
if ( ret.vt == VT_EMPTY )
continue;
strcpy(outfile, filename);
char *p = strchr( outfile, '.' );
if ( p )
*p = 0;
CString str = outfile;
str += "_";
str += sheet.GetName();
if ( str.Find( "说明" ) == -1 &&
str.Find( "说明" ) == -1)
{
str_command[ 0 ].Add( str );
int sign = -1;
ftalk->write( (const char*)&sign, 4 );
int n = str_command[ 0 ].GetSize();
ftalk->write( (const char*)&n, 4 );
strcat(outfile, (LPCSTR)sheet.GetName());
strcat(outfile, ".cxx");
//std::ofstream ar(outfile);
std::ofstream ar;
//ar << (LPCSTR)sheet.GetName() << std::endl;
//Create the SAFEARRAY from the VARIANT ret.
COleSafeArray sa(ret);
SaveAsTalk( ar, sa );
}
}