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[1, 1], ws.Cells[1, iMaxCol]).Font.Name = "Tahoma";
ws.get_Range(ws.Cells[1, 1], 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[1, 1], 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[1, 1], ws.Cells[1,1]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RoomNumber"));
ws.get_Range(ws.Cells[1, 2], ws.Cells[1, 2]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RoomTypeName"));
ws.get_Range(ws.Cells[1, 3], ws.Cells[1, 3]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RoomFloor"));
ws.get_Range(ws.Cells[1, 4], ws.Cells[1, 4]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RatedCount"));
ws.get_Range(ws.Cells[1, 5], ws.Cells[1, 5]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RatedBed"));
ws.get_Range(ws.Cells[1, 6], ws.Cells[1, 6]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "ActuallyCount"));
ws.get_Range(ws.Cells[1, 7], ws.Cells[1, 7]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RoomState"));
ws.get_Range(ws.Cells[1, 8], ws.Cells[1, 8]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RoomMemo"));
xlApp.Cells[1, 1] = "房间号";
xlApp.Cells[1, 2] = "房间类型";
xlApp.Cells[1, 3] = "房间楼层";
xlApp.Cells[1, 4] = "额定人数";
xlApp.Cells[1, 5] = "额定床数";
xlApp.Cells[1, 6] = "实际人数";
xlApp.Cells[1, 7] = "房间状态";
xlApp.Cells[1, 8] = "备注";
}
private void RoomType()

...{
IniFile ini = new IniFile();
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, 1]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn", "RoomTypeName"));
ws.get_Range(ws.Cells[1, 2], ws.Cells[1, 2]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn", "RoomArea"));
ws.get_Range(ws.Cells[1, 3], ws.Cells[1, 3]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn", "RatedBed"));
ws.get_Range(ws.Cells[1, 4], ws.Cells[1, 4]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn", "RatedCount"));
ws.get_Range(ws.Cells[1, 5], ws.Cells[1, 5]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn", "RoomPrice"));
ws.get_Range(ws.Cells[1, 6], ws.Cells[1, 6]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn", "RoomTV"));
ws.get_Range(ws.Cells[1, 7], ws.Cells[1, 7]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn", "RoomPhone"));
ws.get_Range(ws.Cells[1, 8], ws.Cells[1, 8]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn", "RoomAir"));
ws.get_Range(ws.Cells[1, 9], ws.Cells[1, 9]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomTypeExcelColumn", "RoomToilet"));
xlApp.Cells[1, 1] = "客房类型";
xlApp.Cells[1, 2] = "客房面积";
xlApp.Cells[1, 3] = "额定床数";
xlApp.Cells[1, 4] = "额定人数";
xlApp.Cells[1, 5] = "客房价格";
xlApp.Cells[1, 6] = "电视";
xlApp.Cells[1, 7] = "电话";
xlApp.Cells[1, 8] = "空调";
xlApp.Cells[1, 9] = "卫生间";
}
private void RoomSearch()

...{
IniFile ini = new IniFile();
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, 1]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RoomNumber"));
ws.get_Range(ws.Cells[1, 2], ws.Cells[1, 2]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RoomTypeName"));
ws.get_Range(ws.Cells[1, 3], ws.Cells[1, 3]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RoomFloor"));
ws.get_Range(ws.Cells[1, 4], ws.Cells[1, 4]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RatedCount"));
ws.get_Range(ws.Cells[1, 5], ws.Cells[1, 5]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RatedBed"));
ws.get_Range(ws.Cells[1, 6], ws.Cells[1, 6]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "ActuallyCount"));
ws.get_Range(ws.Cells[1, 7], ws.Cells[1, 7]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RoomMemo"));
ws.get_Range(ws.Cells[1, 8], ws.Cells[1, 8]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("RoomExcelColumn", "RoomState"));
xlApp.Cells[1, 1] = "房间号";
xlApp.Cells[1, 2] = "房间类型";
xlApp.Cells[1, 3] = "房间楼层";
xlApp.Cells[1, 4] = "额定人数";
xlApp.Cells[1, 5] = "额定床位";
xlApp.Cells[1, 6] = "实际人数";
xlApp.Cells[1, 7] = "备注";
xlApp.Cells[1, 8] = "房间状态";
}

private void RoomPrearrange()

...{
IniFile ini = new IniFile();
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, 1]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientPrearrangeExC", "ID"));
ws.get_Range(ws.Cells[1, 2], ws.Cells[1, 2]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientPrearrangeExC", "ClientPName"));
ws.get_Range(ws.Cells[1, 3], ws.Cells[1, 3]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientPrearrangeExC", "ClientPPhone"));
ws.get_Range(ws.Cells[1, 4], ws.Cells[1, 4]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientPrearrangeExC", "ClientPRoom"));
ws.get_Range(ws.Cells[1, 5], ws.Cells[1, 5]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientPrearrangeExC", "PrearrangeDate"));
xlApp.Cells[1, 1] = "序号";
xlApp.Cells[1, 2] = "预定者姓名";
xlApp.Cells[1, 3] = "预定电话";
xlApp.Cells[1, 4] = "预定房间";
xlApp.Cells[1, 5] = "预定时间";
}
private void Reckoning()

...{
IniFile ini = new IniFile();
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, 1]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn", "ID"));
ws.get_Range(ws.Cells[1, 2], ws.Cells[1, 2]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn", "ClientID"));
ws.get_Range(ws.Cells[1, 3], ws.Cells[1, 3]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn", "ClientName"));
ws.get_Range(ws.Cells[1, 4], ws.Cells[1, 4]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn", "RoomID"));
ws.get_Range(ws.Cells[1, 5], ws.Cells[1, 5]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn", "InDate"));
ws.get_Range(ws.Cells[1, 6], ws.Cells[1, 6]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn", "CheckDate"));
ws.get_Range(ws.Cells[1, 7], ws.Cells[1, 7]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn", "Settle"));
ws.get_Range(ws.Cells[1, 8], ws.Cells[1, 8]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientRecordExcelCloumn", "ClientMemo"));
xlApp.Cells[1, 1] = "序号";
xlApp.Cells[1, 2] = "客户ID";
xlApp.Cells[1, 3] = "客户姓名";
xlApp.Cells[1, 4] = "房间";
xlApp.Cells[1, 5] = "入住日期";
xlApp.Cells[1, 6] = "退房日期";
xlApp.Cells[1, 7] = "结账金额";
xlApp.Cells[1, 8] = "备注";
}
private void ClientSearch()

...{
Reckoning();
}
private void ClientInfo()

...{
IniFile ini = new IniFile();
ws.get_Range(ws.Cells[1, 1], ws.Cells[1, 1]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn", "ID"));
ws.get_Range(ws.Cells[1, 2], ws.Cells[1, 2]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn", "ClientID"));
ws.get_Range(ws.Cells[1, 3], ws.Cells[1, 3]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn", "ClientName"));
ws.get_Range(ws.Cells[1, 4], ws.Cells[1, 4]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn", "ClientSex"));
ws.get_Range(ws.Cells[1, 5], ws.Cells[1, 5]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn", "ClientAddress"));
ws.get_Range(ws.Cells[1, 6], ws.Cells[1, 6]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn", "ClientPhone"));
ws.get_Range(ws.Cells[1, 7], ws.Cells[1, 7]).ColumnWidth = Convert.ToInt16(ini.IniReadValue("ClientExcelColumn", "ClientMemo"));
xlApp.Cells[1, 1] = "序号";
xlApp.Cells[1, 2] = "客户ID";
xlApp.Cells[1, 3] = "客户姓名";
xlApp.Cells[1, 4] = "性别";
xlApp.Cells[1, 5] = "籍贯";
xlApp.Cells[1, 6] = "电话";
xlApp.Cells[1, 7] = "备注";
}
}
}
=======下面是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