VC中如何将文件保存为EXCEL格式(CSpreadSheet)

2023-12-01 11:18

本文主要是介绍VC中如何将文件保存为EXCEL格式(CSpreadSheet),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

//   
//类名:CExcelAccessDlg   
//功能:对Excel文件进行新建、读写、内部数据行、列、单元格进行   
//      查询,插入,替换等操作,对已有文件进行手工添加的也可以   
//      正确读取等的示例实现   
//作者:徐景周

//组织:未来工作室(Future Studio)   
//日期:2003.5.1   
  
#include "stdafx.h"   
#include "ExcelAccess.h"   
#include "ExcelAccessDlg.h"   
#include "CSpreadSheet.h"   
  
#ifdef _DEBUG   
#define new DEBUG_NEW   
#undef THIS_FILE   
static char THIS_FILE[] = __FILE__;  
#endif   
  
  
// CAboutDlg dialog used for App About   
  
class CAboutDlg : public CDialog  
 
public:  
    CAboutDlg();  
  
// Dialog Data   
    //{{AFX_DATA(CAboutDlg)   
    enum { IDD = IDD_ABOUTBOX };  
    CXPButton   m_OK;  
    //}}AFX_DATA   
  
    // ClassWizard generated virtual function overrides   
    //{{AFX_VIRTUAL(CAboutDlg)   
    protected:  
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support   
    //}}AFX_VIRTUAL   
  
// Implementation   
protected:  
    //{{AFX_MSG(CAboutDlg)   
    //}}AFX_MSG   
    DECLARE_MESSAGE_MAP()  
};  
  
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)  
 
    //{{AFX_DATA_INIT(CAboutDlg)   
    //}}AFX_DATA_INIT   
 
  
void CAboutDlg::DoDataExchange(CDataExchange* pDX)  
 
    CDialog::DoDataExchange(pDX);  
    //{{AFX_DATA_MAP(CAboutDlg)   
    DDX_Control(pDX, IDOK, m_OK);  
    //}}AFX_DATA_MAP   
 
  
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)  
    //{{AFX_MSG_MAP(CAboutDlg)   
        // No message handlers   
    //}}AFX_MSG_MAP   
END_MESSAGE_MAP()  
  
  
// CExcelAccessDlg dialog   
  
CExcelAccessDlg::CExcelAccessDlg(CWnd* pParent )  
    : CDialog(CExcelAccessDlg::IDD, pParent)  
 
    //{{AFX_DATA_INIT(CExcelAccessDlg)   
    m_strRow = _T("");  
    m_strColumn = _T("");  
    //}}AFX_DATA_INIT   
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32   
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);  
 
  
void CExcelAccessDlg::DoDataExchange(CDataExchange* pDX)  
 
    CDialog::DoDataExchange(pDX);  
    //{{AFX_DATA_MAP(CExcelAccessDlg)   
    DDX_Control(pDX, IDC_EDIT_ROW, m_edRow);  
    DDX_Control(pDX, IDC_EDIT_COLUMN, m_edColumn);  
    DDX_Control(pDX, IDOK, m_OK);  
    DDX_Control(pDX, IDCANCEL, m_Cancel);  
    DDX_Control(pDX, ID_WRITEEXCEL, m_Write);  
    DDX_Control(pDX, ID_QUERY, m_Query);  
    DDX_Control(pDX, ID_ABOUT, m_About);  
    DDX_Control(pDX, IDC_CHECK1, m_Check);  
    DDX_Control(pDX, IDC_LISTACCESS, m_AccessList);  
    DDX_Text(pDX, IDC_EDIT_ROW, m_strRow);  
    DDX_Text(pDX, IDC_EDIT_COLUMN, m_strColumn);  
    //}}AFX_DATA_MAP   
 
  
BEGIN_MESSAGE_MAP(CExcelAccessDlg, CDialog)  
    //{{AFX_MSG_MAP(CExcelAccessDlg)   
    ON_WM_SYSCOMMAND()  
    ON_WM_PAINT()  
    ON_WM_QUERYDRAGICON()  
    ON_BN_CLICKED(ID_WRITEEXCEL, OnWriteexcel)  
    ON_BN_CLICKED(ID_QUERY, OnQuery)  
    ON_BN_CLICKED(ID_ABOUT, OnAbout)  
    ON_WM_CTLCOLOR()  
    //}}AFX_MSG_MAP   
END_MESSAGE_MAP()  
  
  
// CExcelAccessDlg message handlers   
//   
//名称:OnInitDialog   
//功能:初始化对话框   
//作者:徐景周

//组织:未来工作室(Future Studio)   
//日期:2003.5.1   
  
BOOL CExcelAccessDlg::OnInitDialog()  
 
    CDialog::OnInitDialog();  
  
    // Add "About..." menu item to system menu.   
  
    // IDM_ABOUTBOX must be in the system command range.   
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);  
    ASSERT(IDM_ABOUTBOX < 0xF000);  
  
    CMenu* pSysMenu = GetSystemMenu(FALSE);  
    if (pSysMenu != NULL)  
    {  
        CString strAboutMenu;  
        strAboutMenu.LoadString(IDS_ABOUTBOX);  
        if (!strAboutMenu.IsEmpty())  
        {  
            pSysMenu->AppendMenu(MF_SEPARATOR);  
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);  
        }  
    }  
  
    // Set the icon for this dialog.  The framework does this automatically   
    //  when the application's main window is not a dialog   
    SetIcon(m_hIcon, TRUE);         // Set big icon   
    SetIcon(m_hIcon, FALSE);        // Set small icon   
      
    // 创建阴影工具提示,并与相应提示控件联系起来   
    m_tooltip.Create(this);  
    m_tooltip.AddTool(GetDlgItem(IDOK), _T("读取



Excel表格数据"),IDI_PINFORMATION);   
       m_tooltip.AddTool(GetDlgItem(ID_WRITEEXCEL), _T(" 写入


新建并写入Excel表格数据"),IDI_PINFORMATION);   
       m_tooltip.AddTool(GetDlgItem(ID_ABOUT), _T("关于"));   
       m_tooltip.AddTool(GetDlgItem(IDCANCEL), _T("退出"));   
       m_tooltip.AddTool(GetDlgItem(ID_QUERY), _T(" 查询


Excel表格中行、列、单元格数据"),IDI_PINFORMATION);   
       m_tooltip.AddTool(GetDlgItem(IDC_LISTACCESS), _T(" 显示


Excel表格数据"),IDI_PINFORMATION);   
       m_tooltip.AddTool(GetDlgI tem(IDC_CHECK1), _T(" 转换


另存为其它文本格式文件"),IDI_PINFORMATION);   
       m_tooltip.AddTool(GetDlgItem(IDC_EDIT_ROW), _T(" 行号


查询该行数据"),IDI_PINFORMATION);   
       m_tooltip.AddTool(GetDlgItem(IDC_EDIT_COLUMN), _T(" 列号


查询该列数据"),IDI_PINFORMATION);   
   
       // 显示图标或位图及字体属性     
       m_tooltip.SetNotify();   
       m_tooltip.SetDefaultFont();   
   
       // 设置背景效果及渐变色     
//   m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_BEGIN, RGB(255, 255, 255));     
//   m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_MID,RGB(240, 247, 250));     
//   m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_END, RGB(192, 192, 200));     
//   m_tooltip.SetEffectBk(14, 10);     
       m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_BEGIN, RGB(255, 255, 223));   
       m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_MID,RGB(192, 192, 172));   
       m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_END, RGB(128, 128, 112));   
       m_tooltip.SetEffectBk(13, 10);   
   
       // 多行显示提示信息     
       m_tooltip.SetBehaviour(PPTOOLTIP_MULTIPLE_SHOW);   
   
   
       // 程序初始启动时,窗体位于最前方     
       SetWindowPos(&wndTopMost,0,0,0,0, SWP_NOMOVE | SWP_NOSIZE);   
           
       return TRUE;   // return TRUE   unless you set the focus to a control     
 
   
void CExcelAccessDlg::OnSysCommand(UINT nID, LPARAM lParam)   
 
       if ((nID & 0xFFF0) == IDM_ABOUTBOX)   
       {   
               CAboutDlg dlgAbout;   
               dlgAbout.DoModal();   
       }   
       else   
       {   
               CDialog::OnSysCommand(nID, lParam);   
       }   
 
   
// If you add a minimize button to your dialog, you will need the code below     
//   to draw the icon.   For MFC applications using the document/view model,     
//   this is automatically done for you by the framework.     
   
void CExcelAccessDlg::OnPaint()     
 
       if (IsIconic())   
       {   
               CPaintDC dc(this); // device context for painting     
   
               SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);   
   
               // Center icon in client rectangle     
               int cxIcon = GetSystemMetrics(SM_CXICON);   
               int cyIcon = GetSystemMetrics(SM_CYICON);   
               CRect rect;   
               GetClientRect(&rect);   
               int x = (rect.Width() - cxIcon + 1) / 2;   
               int y = (rect.Height() - cyIcon + 1) / 2;   
   
               // Draw the icon     
               dc.DrawIcon(x, y, m_hIcon);   
       }   
       else   
       {   
               CDialog::OnPaint();   
       }   
 
   
// The system calls this to obtain the cursor to display while the user drags     
//   the minimized window.     
HCURSOR CExcelAccessDlg::OnQueryDragIcon()   
 
       return (HCURSOR) m_hIcon;   
 
   
//     
//名称:OnOK     
//功能:读取指定Excel文 件内容     
//作者:徐景周

//组织:未来工作室(Future Studio)   
//日期:2003.5.1   
///   
void CExcelAccessDlg::OnOK()   
 
    CSpreadSheet SS("c:\\Test.xls", "TestSheet");  
  
    CStringArray Rows, Column;  
  
    //清空列表框   
    m_AccessList.ResetContent();  
    for (int i = 1; i <= SS.GetTotalRows(); i++)  
    {  
        // 读取一行   
        SS.ReadRow(Rows, i);  
        CString strContents = "";  
        for (int j = 1; j <= Rows.GetSize(); j++)  
        {  
            if(j == 1)  
                strContents = Rows.GetAt(j-1);  
            else  
                strContents = strContents +  " --> " + Rows.GetAt(j-1);  
        }  
  
        m_AccessList.AddString(strContents);  
    }  
 
  
//   
//名称:OnWriteexcel   
//功能:新建并写入Excel文件内容   
//作者:徐景周  
//组织:未来工作室(Future Studio)   
//日期:2003.5.1   
///   
void CExcelAccessDlg::OnWriteexcel()   
 
    // 新建Excel文件名及路径,TestSheet为内部表名   
    CSpreadSheet SS("c:\\Test.xls", "TestSheet");  
  
    CStringArray sampleArray, testRow;  
      
    SS.BeginTransaction();  
      
    // 加入标题   
    sampleArray.RemoveAll();  
    sampleArray.Add("姓名");  
    sampleArray.Add("年龄");  
    SS.AddHeaders(sampleArray);  
      
    // 加入数据   
    CString strName[] = {"徐景周","徐志慧","郭徽","牛英俊","朱小鹏"};  
    CString strAge[]  = {"27","23","28","27","26"};  
    for(int i = 0; i < sizeof(strName)/sizeof(CString); i++)  
    {  
        sampleArray.RemoveAll();  
        sampleArray.Add(strName[i]);  
        sampleArray.Add(strAge[i]);  
        SS.AddRow(sampleArray);  
    }  
      
    // 初始化测试行数据,进行添加、插入及替换数据操作演示   
    for (int k = 1; k <= 2; k++)  
    {  
        testRow.Add("Test");  
    }  
      
    SS.AddRow(testRow);             // 添加到尾部   
    SS.AddRow(testRow, 2);          // 插入新行到第二行   
    SS.AddRow(testRow, 6, true);    // 替换原第四行来新的内容   
      
    SS.Commit();      
      
    if(m_Check.GetCheck())  
        SS.Convert(";");            // 将原Excel文件转换为用分号分隔的文本,并另存为同名文本文件   
  
    AfxMessageBox("文件写入成功!");  
 
  
//   
//名称:OnQuery   
//功能:查询指定Excel文件中行号、列号内容   
//作者:徐景周 
//组织:未来工作室(Future Studio)   
//日期:2003.5.1   
///   
void CExcelAccessDlg::OnQuery()   
 
    CSpreadSheet SS("c:\\Test.xls", "TestSheet");  
  
    CStringArray Rows, Column;  
    CString tempString = "";  
  
    UpdateData();  
  
    if(m_strRow == "" && m_strColumn == "")         // 查询为空   
    {  
        AfxMessageBox("行号、列号不能同时为空!");  
        return;  
    }      
    else if(m_strRow == "" && m_strColumn != "")    // 查询指定列数据   
    {  
        int iColumn = atoi(m_strColumn);  
        int iCols = SS.GetTotalColumns();  
        if(iColumn > iCols)                          // 超出表范围查询时   
        {  
            CString str;  
            str.Format("表中总列数为: %d, ", iCols);  
            AfxMessageBox(str + " 查询列数大于Excel表中总列数,请重新输入!");  
            return;  
        }  
  
        // 读取一列数据,并按行读出   
        if(!SS.ReadColumn(Column, iColumn))  
        {  
            AfxMessageBox(SS.GetLastError());  
            return;  
        }  
  
        CString tmpStr;  
        for (int i = 0; i < Column.GetSize(); i++)  
        {  
            tmpStr.Format("行号: %d, 列号: %d ,内容: %s\n", i+1,iColumn,Column.GetAt(i));  
            tempString += tmpStr;  
        }  
          
        AfxMessageBox(tempString);  
    }  
    else if(m_strRow != "" && m_strColumn == "")     // 查询指定行数数据   
    {  
        int iRow = atoi(m_strRow);  
        int iRows = SS.GetTotalRows();  
          
        if(iRow > iRows)                         // 超出表范围查询时   
        {  
            CString str;  
            str.Format("表中总行数为: %d, ", iRows);  
            AfxMessageBox(str + " 查询行数大于Excel表中总行数,请重新输入!");  
            return;  
        }  
  
        // 读取指定行数据   
        if(!SS.ReadRow(Rows, iRow))  
        {  
            AfxMessageBox(SS.GetLastError());  
            return;  
        }  
  
        CString tmpStr;  
        for (int i = 0; i < Rows.GetSize(); i++)  
        {  
            tmpStr.Format("行号: %d, 列号: %d ,内容: %s\n", iRow, i+1, Rows.GetAt(i));  
            tempString += tmpStr;  
        }  
  
        AfxMessageBox(tempString);  
    }  
    else if(m_strRow != "" && m_strColumn != "")     // 查询指定单元格数据   
    {  
        int iRow = atoi(m_strRow), iColumn = atoi(m_strColumn);  
        int iRows = SS.GetTotalRows(), iCols = SS.GetTotalColumns();   
          
        if(iColumn > iCols)                          // 超出表范围查询时   
        {  
            CString str;  
            str.Format("表中总列数为: %d, ", iCols);  
            AfxMessageBox(str + " 查询列数大于Excel表中总列数,请重新输入!");  
            return;  
        }  
        else if(iRow > iRows)  
        {  
            CString str;  
            str.Format("表中总行数为: %d, ", iRows);  
            AfxMessageBox(str + " 查询行数大于Excel表中总行数,请重新输入!");  
            return;  
        }  
  
        // 读取指定行、列单元格数据   
        if(!SS.ReadCell(tempString, iColumn, iRow))  
        {  
            AfxMessageBox(SS.GetLastError());  
            return;  
        }  
  
        CString str;  
        str.Format("行号: %d, 列号: %d ,内容: %s", iRow,iColumn,tempString);  
        AfxMessageBox(str);  
    }  
      
 
  
//   
//名称:OnAbout   
//功能:关于对话框   
//作者:徐景周
//组织:未来工作室(Future Studio)   
//日期:2003.5.1   
///   
void CExcelAccessDlg::OnAbout()   
 
    CAboutDlg dlg;  
    dlg.DoModal();  
 
  
//   
//名称:OnCtlColor   
//功能:设置各控件前景、背景色   
//作者:徐景周

//组织:未来工作室(Future Studio)   
//日期:2003.5.1   
  
HBRUSH CExcelAccessDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)   
    
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
      
    if(nCtlColor==CTLCOLOR_LISTBOX)  
    {  
        //pDC->SetBkMode(TRANSPARENT);   
        pDC->SetTextColor(RGB(0,0,0));  
        pDC->SetBkColor(RGB(233,233,220));  
        HBRUSH b=CreateSolidBrush(RGB(233,233,220));  
        return b;  
    }  
    else if(nCtlColor==CTLCOLOR_SCROLLBAR)  
    {  
          
      
    }  
    else if(nCtlColor==CTLCOLOR_EDIT)  
    {  
          
          
    }  
    else if(nCtlColor==CTLCOLOR_STATIC)  
    {  
      
          
    }  
    else if(nCtlColor==CTLCOLOR_DLG)  
    {  
          
          
    }  
      
    return hbr;  
 
  
//   
//名称:PreTranslateMessage   
//功能:阴影工具提示的消息传递   
//作者:徐景周

//组织:未来工作室(Future Studio)   
//日期:2003.5.1   
  
BOOL CExcelAccessDlg::PreTranslateMessage(MSG* pMsg)   
 
    // 为阴影工具提示加入鼠标事件传递   
    m_tooltip.RelayEvent(pMsg);   
  
    return CDialog::PreTranslateMessage(pMsg);  
}

这篇关于VC中如何将文件保存为EXCEL格式(CSpreadSheet)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/440956

相关文章

VC网络协议

// PCControlDlg.cpp : 实现文件//#include "stdafx.h"#include "PCControl.h"#include "PCControlDlg.h"#include "afxdialogex.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// 用于应用程序“关于”菜单项的 CAboutDlg 对话框#ifde

easyui同时验证账户格式和ajax是否存在

accountName: {validator: function (value, param) {if (!/^[a-zA-Z][a-zA-Z0-9_]{3,15}$/i.test(value)) {$.fn.validatebox.defaults.rules.accountName.message = '账户名称不合法(字母开头,允许4-16字节,允许字母数字下划线)';return fal

C#关闭指定时间段的Excel进程的方法

private DateTime beforeTime;            //Excel启动之前时间          private DateTime afterTime;               //Excel启动之后时间          //举例          beforeTime = DateTime.Now;          Excel.Applicat

[数据集][目标检测]血细胞检测数据集VOC+YOLO格式2757张4类别

数据集格式:Pascal VOC格式+YOLO格式(不包含分割路径的txt文件,仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数):2757 标注数量(xml文件个数):2757 标注数量(txt文件个数):2757 标注类别数:4 标注类别名称:["Platelets","RBC","WBC","sickle cell"] 每个类别标注的框数:

一步一步将PlantUML类图导出为自定义格式的XMI文件

一步一步将PlantUML类图导出为自定义格式的XMI文件 说明: 首次发表日期:2024-09-08PlantUML官网: https://plantuml.com/zh/PlantUML命令行文档: https://plantuml.com/zh/command-line#6a26f548831e6a8cPlantUML XMI文档: https://plantuml.com/zh/xmi

excel翻译软件有哪些?如何高效提翻译?

你是否曾在面对满屏的英文Excel表格时感到头疼?项目报告、数据分析、财务报表... 当这些重要的信息被语言壁垒阻挡时,效率和理解度都会大打折扣。别担心,只需3分钟,我将带你轻松解锁excel翻译成中文的秘籍。 无论是职场新人还是老手,这一技巧都将是你的得力助手,让你在信息的海洋中畅游无阻。 方法一:使用同声传译王软件 同声传译王是一款专业的翻译软件,它支持多种语言翻译,可以excel

终于解决了excel操作及cspreadsheet.h问题

困扰多日的excel操作问题终于解决:利用cspreadsheet.h!在vs2005下,不能直接应用cspreadsheet.h,所以必须解决些问题先。 首先, 出现暴多错误。解决UNICODE问题,全部添加L。 [1] +++++++++++++++++++ 其次, 出现问题: error   C2664:   &apos;SQLGetInstalledDriversW &apos;

关于使用cspreadsheet读写EXCEL表格数据的问题

前几天项目有读写EXCEL表格的需求,我就找了大概有几种,大致分为:COM方法、ODBC方法、OLE方法、纯底层格式分析方法。由于COM方法要求必须安装有OFFICE的EXCEL组件,纯底层格式分析方法又很多功能需要自行去完善,所有最终选择了数据库的方法,用数据库的方法去存取xls格式的数据。网上有一个高手写的CSpreedSheet,看了一下提供的接口,感觉挺好用的。在使用的过程中发现几个

下载/保存/读取 文件,并转成流输出

最近对文件的操作又熟悉了下;现在记载下来:学习在于 坚持!!!不以细小而不为。 实现的是:文件的下载、文件的保存到SD卡、文件的读取输出String 类型、最后是文件转换成流输出;一整套够用了; 重点: 1:   操作网络要记得开线程; 2:更新网络获取的数据 切记用Handler机制; 3:注意代码的可读性(这里面只是保存到SD卡,在项目中切记要对SD卡的有无做判断,然后再获取路径!)

FFmpeg系列-视频解码后保存帧图片为ppm

在正常开发中遇到花屏时怎么处理呢?可以把解码后的数据直接保存成帧图片保存起来,然后直接看图片有没有花屏来排除是否是显示的问题,如果花屏,则代表显示无问题,如果图片中没有花屏,则可以往显示的方向去排查了。 void saveFrame(AVFrame* pFrame, int width, int height, int iFrame){FILE *pFile;char szFilename[