新手入门.刚刚学会如何生成EXCEL文件,但发现生成的EXCEL文件的文件名总是book1.如何才能生成自己需要的文件名呢.我的代码://////////////////////////////////////////////////////
try
{
objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add( Missing.Value);
objSheets = objBook.Worksheets;
objSheet = (Excel._Worksheet)objSheets.get_Item(1);
//Get the range where the starting cell has the address
//m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
range = objSheet.get_Range("A1", Missing.Value);
range = range.get_Resize(6, 3);
//Create an array.
string[,] saRet = new string[6, 3];
//Fill the array.
saRet[0, 0]="用户名";
saRet[0, 1]="备注";
saRet[0, 2]="密码";
for (int iRow = 1; iRow < 6; iRow++)
{
saRet[iRow, 0] = (string)dataSet1.Tables["class_user"].Rows[iRow-1]["name"];
saRet[iRow, 1] = (string)dataSet1.Tables["class_user"].Rows[iRow-1]["context"];
saRet[iRow, 2] = (string)dataSet1.Tables["class_user"].Rows[iRow-1]["pwd"];
}
//Set the range value to the array.
range.set_Value(Missing.Value, saRet );
//Return control of Excel to the user.
objApp.Visible = true;
objApp.UserControl = true;
}
catch( Exception theException )
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat( errorMessage, theException.Message );
errorMessage = String.Concat( errorMessage, " Line: " );
errorMessage = String.Concat( errorMessage, theException.Source );
MessageBox.Show( errorMessage, "Error" );
}
try
{
objApp = new Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add( Missing.Value);
objSheets = objBook.Worksheets;
objSheet = (Excel._Worksheet)objSheets.get_Item(1);
//Get the range where the starting cell has the address
//m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
range = objSheet.get_Range("A1", Missing.Value);
range = range.get_Resize(6, 3);
//Create an array.
string[,] saRet = new string[6, 3];
//Fill the array.
saRet[0, 0]="用户名";
saRet[0, 1]="备注";
saRet[0, 2]="密码";
for (int iRow = 1; iRow < 6; iRow++)
{
saRet[iRow, 0] = (string)dataSet1.Tables["class_user"].Rows[iRow-1]["name"];
saRet[iRow, 1] = (string)dataSet1.Tables["class_user"].Rows[iRow-1]["context"];
saRet[iRow, 2] = (string)dataSet1.Tables["class_user"].Rows[iRow-1]["pwd"];
}
//Set the range value to the array.
range.set_Value(Missing.Value, saRet );
//Return control of Excel to the user.
objApp.Visible = true;
objApp.UserControl = true;
}
catch( Exception theException )
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat( errorMessage, theException.Message );
errorMessage = String.Concat( errorMessage, " Line: " );
errorMessage = String.Concat( errorMessage, theException.Source );
MessageBox.Show( errorMessage, "Error" );
}
objBook = objBooks.Add( Missing.Value);
objBook.Name = "MyBookName";
改名字不是在这段代码里面
是在保存的地方 saveas()里面第一个参数