当前位置: 首页 > 工具软件 > XLSWriter > 使用案例 >

MFC CFileDialog类 + xlswriter库,数据写入Excel

王波
2023-12-01

CFileDialog类

CFileDialog构造函数

CFileDialog::CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL,

        LPCTSTR lpszFileName = NULL,

       DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,

       LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL );

bOpenFileDialog
为TRUE则显示打开对话框,为FALSE则显示保存对话文件对话框。

lpszDefExt
指定默认的文件扩展名。

lpszFileName
指定默认的文件名。

dwFlags
指明一些特定风格。 (默认OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT)

lpszFilter
指明可供选择的文件类型和相应的扩展名。

pParentWnd
为父窗口指针。

一些使用方法

CString CFileDialog::GetPathName( ) 得到完整的文件名,包括目录名和扩展名如

CString CFileDialog::GetFileName( ) 得到完整的文件名,包括扩展名如

CString CFileDialog::GetExtName( ) 得到完整的文件扩展名

CString CFileDialog::GetFileTitle ( ) 得到完整的文件名,不包括目录名和扩展名如

POSITION CFileDialog::GetStartPosition( ) 对于选择了多个文件的情况得到第一个文件位置。

CString CFileDialog::GetNextPathName( POSITION& pos ) 对于选择了多个文件的情况得到下一个文件位置,同时返回当前文件名。但必须已经调用过POSITION CFileDialog::GetStartPosition( )来得到最初的POSITION变量。

使用

CFileDialog dlg(FALSE,_T("xlsx"),NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("Export Excel File(*.xlsx)|*.xlsx||"));
	if(dlg.DoModal() == IDOK)
	{
		CString fileChooseName = dlg.GetPathName();
		wParamStruct wps_AlarmExportExcel(DLG_CLIENT, ALARM_EXPORT_EXCEL);
		::SendMessage(theApp.m_pMainWnd->m_hWnd, HANDLEMESSAGE, (WPARAM)&wps_AlarmExportExcel, (LPARAM)&fileChooseName);
	}

xlswriter库

workbook.h
工作簿是libxlsxwriter库公开的主要对象。它表示在Excel中看到的整个电子表格,在内部它表示写在磁盘上的Excel文件。

例子:

 #include "xlsxwriter.h"
 *
 *     int main() {
 */*
  @brief Create a new workbook object, and set the workbook options.
  @param filename The name of the new Excel file to create.
  @param options  Workbook options.
  @return A lxw_workbook instance.
 */
         lxw_workbook  *workbook  = workbook_new("filename.xlsx");
  /*
  @brief Add a new chartsheet to a workbook.
  @param workbook  Pointer to a lxw_workbook instance.
  @param sheetname Optional chartsheet name, defaults to Chart1, etc.
  @return A lxw_chartsheet object.
 */
 *         lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
 *
 /*
 
 */      worksheet_write_string(worksheet, 0, 0, "Hello Excel", NULL);
 *
 *         return workbook_close(workbook);
 *     }
/*
typedef struct lxw_workbook {

    FILE *file;
    struct lxw_sheets *sheets;
    struct lxw_worksheets *worksheets;
    struct lxw_chartsheets *chartsheets;
    struct lxw_worksheet_names *worksheet_names;
    struct lxw_chartsheet_names *chartsheet_names;
    struct lxw_charts *charts;
    struct lxw_charts *ordered_charts;
    struct lxw_formats *formats;
    struct lxw_defined_names *defined_names;
    lxw_sst *sst;
    lxw_doc_properties *properties;
    struct lxw_custom_properties *custom_properties;

    char *filename;
    lxw_workbook_options options;

    uint16_t num_sheets;
    uint16_t num_worksheets;
    uint16_t num_chartsheets;
    uint16_t first_sheet;
    uint16_t active_sheet;
    uint16_t num_xf_formats;
    uint16_t num_format_count;
    uint16_t drawing_count;

    uint16_t font_count;
    uint16_t border_count;
    uint16_t fill_count;
    uint8_t optimize;

    uint8_t has_png;
    uint8_t has_jpeg;
    uint8_t has_bmp;

    lxw_hash_table *used_xf_formats;

    char *vba_project;
    char *vba_codename;

} lxw_workbook;
*/

worksheet.h

lxw_error worksheet_write_string(lxw_worksheet *worksheet,
                                 lxw_row_t row,
                                 lxw_col_t col, const char *string,
                                 lxw_format *format);
/**
 * @brief Write a formula to a worksheet cell.
 *
 * @param worksheet pointer to a lxw_worksheet instance to be updated.
 * @param row       The zero indexed row number.
 * @param col       The zero indexed column number.
 * @param formula   Formula string to write to cell.
 * @param format    A pointer to a Format instance or NULL.
 *
 * @return A #lxw_error code.
*/
lxw_error worksheet_set_column(lxw_worksheet *worksheet,
                               lxw_col_t first_col,
                               lxw_col_t last_col,
                               double width, lxw_format *format);

/**
 * @brief Set the properties for one or more columns of cells with options.
 *
 * @param worksheet Pointer to a lxw_worksheet instance to be updated.
 * @param first_col The zero indexed first column.
 * @param last_col  The zero indexed last column.
 * @param width     The width of the column(s).
 * @param format    A pointer to a Format instance or NULL.
 * @param options   Optional row parameters: hidden, level, collapsed.
 *
 * The `%worksheet_set_column_opt()` function  is the same as
 * `worksheet_set_column()` with an additional `options` parameter.
 *
 */
 

format.h

void format_set_bold(lxw_format *format);

/**
 * @brief Turn on italic for the format font.
 *
 * @param format Pointer to a Format instance.
 *
 * Set the italic property of the font:
 *
 * @code
 *     format = workbook_add_format(workbook);
 *     format_set_italic(format);
 *
 *     worksheet_write_string(worksheet, 0, 0, "Italic Text", format);
 * @endcode
 *
 * @image html format_font_italic.png
 */
 void format_set_align(lxw_format *format, uint8_t alignment);

/**
 * @brief Wrap text in a cell.
 *
 * Turn text wrapping on for text in a cell.
 *
 * @code
 *     format = workbook_add_format(workbook);
 *     format_set_text_wrap(format);
 **/
 void format_set_font_size(lxw_format *format, double size);

/**
 * @brief Set the color of the font used in the cell.
 *
 * @param format Pointer to a Format instance.
 * @param color  The cell font color.
 **/
 void format_set_font_name(lxw_format *format, const char *font_name);
 
/**
 * @brief Set the size of the font used in the cell.
 *
 * @param format Pointer to a Format instance.
 * @param size   The cell font size.
 **/
 void format_set_bg_color(lxw_format *format, lxw_color_t color);

/**
 * @brief Set the pattern foreground color for a cell.
 *
 * @param format Pointer to a Format instance.
 * @param color  The cell pattern foreground  color.
 **/
 

代码

/* Create a new workbook and add a worksheet. */
	lxw_workbook  *workbook  = workbook_new(CT2CA(fileChooseName));
	lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
	//enum{PLATE_NO, CREATE_TIME, STATUS, REASON, REMARK, COUNT};
	CString strListNo, strListCreateTime,  strListStatus, strListReason, strListRemark;

	/* Change the column width for clarity. */
	worksheet_set_column(worksheet, 0, PLATE_NO,    20, NULL);
	worksheet_set_column(worksheet, 0, CREATE_TIME, 20, NULL);
	worksheet_set_column(worksheet, 0, STATUS,      20, NULL);
	worksheet_set_column(worksheet, 0, REASON,      20, NULL);
	worksheet_set_column(worksheet, 0, REMARK,      20,  NULL);

	lxw_format *format = workbook_add_format(workbook);
	/* Set the bold, font_size property for the format */
	format_set_bold(format);
	format_set_align(format, LXW_ALIGN_CENTER);
	format_set_font_size(format, 16);  //设置18号字体大小
	format_set_font_name(format, "Arial"); //设置字体
	format_set_bg_color(format,LXW_COLOR_GRAY);

	worksheet_write_string(worksheet, 0, PLATE_NO,    "Plate No",   format);		
	worksheet_write_string(worksheet, 0, CREATE_TIME, "Create Time",format);		
	worksheet_write_string(worksheet, 0, STATUS,     "Status",      format);		
	worksheet_write_string(worksheet, 0, REASON,     "Reason",      format);				
	worksheet_write_string(worksheet, 0, REMARK,     "Remark",      format);	

	// 设置内容单元格cell的格式
	lxw_format *format_cell = workbook_add_format(workbook);
	/* Set the bold, font_size property for the format */
	format_set_align(format_cell, LXW_ALIGN_CENTER);
	format_set_font_size(format_cell, 9);  //设置9号字体大小
	format_set_font_name(format_cell, "Arial"); //设置字体

	for (int i=0; i<itemCount; i++)
	{
		strListNo.Empty(); 
		strListCreateTime.Empty(); 
		strListStatus.Empty(); 
		strListReason.Empty(); 
		strListRemark.Empty(); 


		strListNo       = m_ListCtrl.GetItemText(i, PLATE_NO);  
		strListCreateTime    = m_ListCtrl.GetItemText(i, CREATE_TIME);
		strListStatus        = m_ListCtrl.GetItemText(i, STATUS);    
		strListReason        = m_ListCtrl.GetItemText(i, REASON);  
		strListRemark        = m_ListCtrl.GetItemText(i, REMARK);  

		worksheet_write_string(worksheet, i+1, PLATE_NO,   CT2CA(strListNo ),      format_cell);		
		worksheet_write_string(worksheet, i+1, CREATE_TIME, CT2CA(strListCreateTime),   format_cell);		
		worksheet_write_string(worksheet, i+1, STATUS,     CT2CA(strListStatus),        format_cell);		
		worksheet_write_string(worksheet, i+1, REASON,     CT2CA(strListReason),        format_cell);		
		worksheet_write_string(worksheet, i+1, REMARK,     CT2CA(strListRemark),        format_cell);			

	}
	workbook_close(workbook);
	MessageBox(_T("Export Excel OK"));
 类似资料: