导出Excel表

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;
using Microsoft.Office.Interop.Excel;

namespace BoyangMIS
{
   
    
class OutDataClass
    
{
        
//DataSet dst = new DataSet();
       
        
public Microsoft.Office.Interop.Excel.Application xlApp;
        Workbook wb;
        Worksheet ws;

        
public string modName;
        
public  DataSet dstExcel = new DataSet();
        
public OutDataClass(DataSet _dstExcel, string  _modName)
        
{
            
this.dstExcel = _dstExcel;
            
this.modName = _modName;
        }

        
//public int ModName
        
//{
        
//    get
        
//    {
        
//        return modName;
        
//    }
        
//    set
        
//    {
        
//        modName = value;
        
//    }
        
//}
        
//public DataSet Dstexcel
        
//{
        
//    get
        
//    {
        
//        return dstExcel;
        
//    }
        
//    set
        
//    {
        
//        dstExcel = value;
        
//    }
        
//}
        public  void OutDataExcel()
        
{
            
try
            
{
                xlApp 
= new Microsoft.Office.Interop.Excel.Application();
                wb 
= xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
               ws 
= (Worksheet)wb.Worksheets[1];

                
int iMaxRow = dstExcel.Tables[0].Rows.Count;
                
int iMaxCol = dstExcel.Tables[0].Columns.Count;
                
//设置公共格式
                ws.get_Range(ws.Cells[11], ws.Cells[1, iMaxCol]).Font.Name = "Tahoma";
                ws.get_Range(ws.Cells[
11], ws.Cells[1, iMaxCol]).Font.Bold = true;
                
//ws.get_Range(ws.Cells[1, 1], ws.Cells[1, iMaxCol]).Font.Size = 10;

                
                ws.get_Range(ws.Cells[
11], ws.Cells[iMaxRow + 1, iMaxCol]).Borders.LineStyle = 1;

                xlApp.Cells.Font.Size 
= 10;
                

                
//设置标题
                if (modName == "Room")
                    Room();
                
else if (modName == "RoomType")
                    RoomType();
                
else if (modName == "RoomSearch")
                    RoomSearch();
                
else if (modName == "RoomPrearrange")
                    RoomPrearrange();
                
else if (modName == "Reckoning")
                    Reckoning();
                
else if (modName == "ClientSearch")
                    ClientSearch();
                
else if (modName == "ClientInfo")
                    ClientInfo();

                
//填充数据
                for (int iRow = 0; iRow < iMaxRow; iRow++)
                
{
                    
for (int iCol = 0; iCol < iMaxCol; iCol++)
                    
{
                        xlApp.Cells[iRow 
+ 2, iCol + 1= dstExcel.Tables[0].Rows[iRow][iCol].ToString();
                    }

                }

                
//保存Execl
                
//xlApp.Save("C:/Room.xls");
                ////打开EXCEL
                xlApp.Visible = true;
            }

            
catch (Exception exp)
            
{
                MessageBox.Show(exp.ToString());
                
return;
            }


        }

        
private void Room()
        
{
            IniFile ini 
= new IniFile();
            ws.get_Range(ws.Cells[
11], ws.Cells[1,1]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RoomNumber"));
            ws.get_Range(ws.Cells[
12], ws.Cells[12]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RoomTypeName"));
            ws.get_Range(ws.Cells[
13], ws.Cells[13]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RoomFloor"));
            ws.get_Range(ws.Cells[
14], ws.Cells[14]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RatedCount"));
            ws.get_Range(ws.Cells[
15], ws.Cells[15]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RatedBed"));
            ws.get_Range(ws.Cells[
16], ws.Cells[16]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""ActuallyCount"));
            ws.get_Range(ws.Cells[
17], ws.Cells[17]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RoomState"));
            ws.get_Range(ws.Cells[
18], ws.Cells[18]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RoomMemo"));
                xlApp.Cells[
11= "房间号";
                xlApp.Cells[
12= "房间类型";
                xlApp.Cells[
13= "房间楼层";
                xlApp.Cells[
14= "额定人数";
                xlApp.Cells[
15= "额定床数";
                xlApp.Cells[
16= "实际人数";
                xlApp.Cells[
17= "房间状态";
                xlApp.Cells[
18= "备注";
        }

        
private void RoomType()
        
{
            IniFile ini 
= new IniFile();
            ws.get_Range(ws.Cells[
11], ws.Cells[11]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn""RoomTypeName"));
            ws.get_Range(ws.Cells[
12], ws.Cells[12]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn""RoomArea"));
            ws.get_Range(ws.Cells[
13], ws.Cells[13]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn""RatedBed"));
            ws.get_Range(ws.Cells[
14], ws.Cells[14]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn""RatedCount"));
            ws.get_Range(ws.Cells[
15], ws.Cells[15]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn""RoomPrice"));
            ws.get_Range(ws.Cells[
16], ws.Cells[16]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn""RoomTV"));
            ws.get_Range(ws.Cells[
17], ws.Cells[17]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn""RoomPhone"));
            ws.get_Range(ws.Cells[
18], ws.Cells[18]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn""RoomAir"));
            ws.get_Range(ws.Cells[
19], ws.Cells[19]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn""RoomToilet"));
            xlApp.Cells[
11= "客房类型";
            xlApp.Cells[
12= "客房面积";
            xlApp.Cells[
13= "额定床数";
            xlApp.Cells[
14= "额定人数";
            xlApp.Cells[
15= "客房价格";
            xlApp.Cells[
16= "电视";
            xlApp.Cells[
17= "电话";
            xlApp.Cells[
18= "空调";
            xlApp.Cells[
19= "卫生间";
        }

        
private void RoomSearch()
        
{
            IniFile ini 
= new IniFile();
            ws.get_Range(ws.Cells[
11], ws.Cells[11]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RoomNumber"));
            ws.get_Range(ws.Cells[
12], ws.Cells[12]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RoomTypeName"));
            ws.get_Range(ws.Cells[
13], ws.Cells[13]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RoomFloor"));
            ws.get_Range(ws.Cells[
14], ws.Cells[14]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RatedCount"));
            ws.get_Range(ws.Cells[
15], ws.Cells[15]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RatedBed"));
            ws.get_Range(ws.Cells[
16], ws.Cells[16]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""ActuallyCount"));
            ws.get_Range(ws.Cells[
17], ws.Cells[17]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RoomMemo"));
            ws.get_Range(ws.Cells[
18], ws.Cells[18]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn""RoomState"));
            xlApp.Cells[
11= "房间号";
            xlApp.Cells[
12= "房间类型";
            xlApp.Cells[
13= "房间楼层";
            xlApp.Cells[
14= "额定人数";
            xlApp.Cells[
15= "额定床位";
            xlApp.Cells[
16= "实际人数";
            xlApp.Cells[
17= "备注";
            xlApp.Cells[
18= "房间状态";
          
        }


        
private void RoomPrearrange()
        
{
            IniFile ini 
= new IniFile();
            ws.get_Range(ws.Cells[
11], ws.Cells[11]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientPrearrangeExC""ID"));
            ws.get_Range(ws.Cells[
12], ws.Cells[12]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientPrearrangeExC""ClientPName"));
            ws.get_Range(ws.Cells[
13], ws.Cells[13]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientPrearrangeExC""ClientPPhone"));
            ws.get_Range(ws.Cells[
14], ws.Cells[14]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientPrearrangeExC""ClientPRoom"));
            ws.get_Range(ws.Cells[
15], ws.Cells[15]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientPrearrangeExC""PrearrangeDate"));
            xlApp.Cells[
11= "序号";
            xlApp.Cells[
12= "预定者姓名";
            xlApp.Cells[
13= "预定电话";
            xlApp.Cells[
14= "预定房间";
            xlApp.Cells[
15= "预定时间";
        }

        
private void Reckoning()
        
{
            IniFile ini 
= new IniFile();
            ws.get_Range(ws.Cells[
11], ws.Cells[11]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn""ID"));
            ws.get_Range(ws.Cells[
12], ws.Cells[12]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn""ClientID"));
            ws.get_Range(ws.Cells[
13], ws.Cells[13]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn""ClientName"));
            ws.get_Range(ws.Cells[
14], ws.Cells[14]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn""RoomID"));
            ws.get_Range(ws.Cells[
15], ws.Cells[15]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn""InDate"));
            ws.get_Range(ws.Cells[
16], ws.Cells[16]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn""CheckDate"));
            ws.get_Range(ws.Cells[
17], ws.Cells[17]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn""Settle"));
            ws.get_Range(ws.Cells[
18], ws.Cells[18]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn""ClientMemo"));
            xlApp.Cells[
11= "序号";
            xlApp.Cells[
12= "客户ID";
            xlApp.Cells[
13= "客户姓名";
            xlApp.Cells[
14= "房间";
            xlApp.Cells[
15= "入住日期";
            xlApp.Cells[
16= "退房日期";
            xlApp.Cells[
17= "结账金额";
            xlApp.Cells[
18= "备注";
            
        }

        
private void ClientSearch()
        
{
            Reckoning();
        }

        
private void ClientInfo()
        
{
            IniFile ini 
= new IniFile();
            ws.get_Range(ws.Cells[
11], ws.Cells[11]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn""ID"));
            ws.get_Range(ws.Cells[
12], ws.Cells[12]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn""ClientID"));
            ws.get_Range(ws.Cells[
13], ws.Cells[13]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn""ClientName"));
            ws.get_Range(ws.Cells[
14], ws.Cells[14]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn""ClientSex"));
            ws.get_Range(ws.Cells[
15], ws.Cells[15]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn""ClientAddress"));
            ws.get_Range(ws.Cells[
16], ws.Cells[16]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn""ClientPhone"));
            ws.get_Range(ws.Cells[
17], ws.Cells[17]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn""ClientMemo"));
            xlApp.Cells[
11= "序号";
            xlApp.Cells[
12= "客户ID";
            xlApp.Cells[
13= "客户姓名";
            xlApp.Cells[
14= "性别";
            xlApp.Cells[
15= "籍贯";
            xlApp.Cells[
16= "电话";
            xlApp.Cells[
17= "备注";
           
        }

    }

}

=======下面是parameter.ini文件内容===============

关于操作INI文件Class,请查看我的"简单操作INI文件"文章或联系我

[DataGridView]
pagesize = 10

[ControlPar]
Address = 北京,上海
Address1 = "北京"
Address2 = "上海"
Address3 = "天津"
Address4 = "重庆"


[RoomExcelColumn]
RoomNumber   = 5
RoomTypeName = 8
RoomFloor    = 7
RatedCount   = 7
RatedBed     = 7
ActuallyCount= 7
RoomState    = 7
RoomMemo     = 25


[RoomTypeExcelColumn]
RoomTypeName = 8
RoomArea     = 7
RatedBed     = 7
RatedCount   = 7
RoomPrice    = 7
RoomTV       = 7
RoomPhone    = 7
RoomAir      = 7
RoomToilet   = 7

[ClientRecordExcelCloumn]
ID           = 7
ClientID     = 20
ClientName   = 8
RoomID       = 7
InDate       = 15
CheckDate    = 15
Settle       = 7
ClientMemo   = 25

[ClientPrearrangeExC]
ID            = 7
ClientPName   = 8
ClientPPhone  = 10
ClientPRoom   = 7
PrearrangeDate= 15

[ClientExcelColumn]
ID           = 7
ClientID     = 20
ClientName   = 8
ClientSex    = 7
ClientAddress= 7
ClientPhone  = 10
ClientMemo   = 25

[Time]
;预定提示时间以分钟计算
Prearrange   = 30

### 回答1: 要将MySQL Workbench中的导出Excel格,可以按照以下步骤操作: 1. 在MySQL Workbench中打开要导出的数据库连接。 2. 选择要导出格,右键点击格名称,选择“Table Data Export Wizard”。 3.导出向导中,选择要导出格和导出格式为“Microsoft Excel”。 4. 配置导出选项,包括文件名、文件路径、导出数据的范围等。 5. 点击“Export”按钮,开始导出Excel格。 6. 导出完成后,可以在指定的文件路径中找到导出Excel格文件。 希望以上步骤能够帮助您成功导出MySQL Workbench中的格为Excel格。 ### 回答2: MySQL Workbench是一款广泛使用的MySQL数据库设计和管理工具。很多人都希望从MySQL Workbench中导出数据到Excel格,本文将详细介绍如何进行操作。 首先,在MySQL Workbench中打开要导出的数据库,并选择要导出的数据。在数据上右键单击,选择“Table Data Export Wizard”选项,在弹出的窗口中选择要导出的数据行范围和选定要导出数据的列。 在选择要导出的列后,单击“下一步”按钮,然后选择导出格式为“Excel”。然后,选择要导出Excel文件的保存路径和名称,单击“下一步”按钮。 在下一步中,您可以设置一些高级选项,例如分隔符和字符集。单击“下一步”按钮,并在确认页上检查设置,然后单击“导出”按钮,将数据导出Excel中。 总的来说,MySQL Workbench导出数据到Excel非常简单,只需选择要导出的数据行和列,并将其配置为Excel格式。在导出过程中,可以调整相应的高级设置以满足导出需求。这种方法是速度相对较快的,同时也可以节省大量的手动操作时间,非常实用。 ### 回答3: MySQL是一个开源的关系型数据库管理系统,MySQL Workbench是MySQL官方提供的一个强大的可视化数据库建模工具。MySQL Workbench内置了导出数据的功能,用户可以将数据以Excel格的形式导出,在Excel中方便地进行数据分析和处理。 在MySQL Workbench中导出Excel格有以下几个步骤: 第一步,连接到需要导出数据的数据库。在MySQL Workbench中,选择“连接”菜单栏中的“新建连接”,填写连接信息并点击“测试连接”按钮,连接成功后点击“确定”按钮保存连接。 第二步,选择需要导出的数据。在MySQL Workbench中,展开连接后的数据库,在左侧“SCHEMAS”栏选择需要导出的数据库,展开该数据库后选择需要导出的数据。 第三步,选择数据中的数据行。在MySQL Workbench中,通过执行SQL查询或者在数据中手动勾选需要导出的数据行。 第四步,导出Excel格。在MySQL Workbench中,选择“查询”菜单栏中的“导出结果集”,在导出向导中选择“Microsoft Excel”格式,填写导出文件路径并选择需要导出的字段,最后保存导出设置并点击“导出”按钮即可。 在导出Excel格过程中,需要注意以下几个问题: 1. 导出格的格式选择:MySQL Workbench支持多种导出格式,用户需要根据实际需求选择。 2. 导出数据的范围选择:用户可以通过手动勾选数据行或者执行SQL语句来选择需要导出的数据范围。 3. 导出数据格的文件路径选择:用户需要选择一个合适的文件路径来保存导出Excel格。 4. 导出数据的字段选择:用户可以选择需要导出的字段以减少Excel格中的数据冗余。 总之,MySQL Workbench内置的导出数据功能为用户提供了一个方便快捷的数据导出手段。用户可以根据实际需求选择合适的导出格式、数据范围、文件路径和字段范围,在Excel中进行方便的数据分析和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值