Free Support Forum - aspose.com

Opening an excel sheet using aspose.cells

Hi All,

I am new to aspose.cells can you please help me regarding an issue .

I have method as follows:

private void button1_Click(object sender, EventArgs e)

{

Workbook wb = new Workbook();

FileStream fstream = new FileStream("C:\\search1.xls", FileMode.Open);

//Opening the Excel file through the file stream

wb.Open(fstream);

//Adding a new worksheet to the Workbook object

wb.Worksheets.Add();

//Obtaining the reference of the newly added worksheet by passing its sheet index

Worksheet worksheet = wb.Worksheets[0];

//Setting the name of the newly added worksheet

worksheet.Name = "My Worksheet";

//Saving the Excel file

fstream.Close();

wb.Save("C:\\search2.xls", FileFormatType.Excel2003);

System.Diagnostics.Process.Start("C:\\search2.xls");

}

But,when the new excel is opened it is opening with name myworksheet but there are two additional sheets like shhet3,sheet4... coming along with it and other is license sheet (i think this because i am using trial version).

Can yo help me in opening only myworksheet and discarding the other two.

Thank You,

Bindu.

Hello!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for your inquiry. I have moved this question to Aspose.Cells forum.

Regards,

Hi Bindu,

Thanks for considering Aspose.

Your codes show that you are manipulating an existing excel file "search1.xls" resided on c: drive. Moreover, your template file i.e. "search1.xls" may already have two sheets, so, when you add a new worksheet to the wokbook, the sheet count would be 3. Moreover, the evaluation watermark sheet is automatically added if you are using evaluation version of the component (not using the license file).

Well, if you are using an existing template and want to manipulate it, your code needs to be corrected as follows:

Workbook wb = new Workbook();
FileStream fstream = new FileStream("c:\\search1.xls", FileMode.Open);
//Opening the Excel file through the file stream
wb.Open(fstream);
//Adding a new worksheet to the Workbook object
int i = wb.Worksheets.Add();
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = wb.Worksheets[i];
//Setting the name of the newly added worksheet
worksheet.Name = "My Worksheet";
//Saving the Excel file
fstream.Close();
wb.Save("c:\\search2.xls", FileFormatType.Excel2003);
System.Diagnostics.Process.Start("c:\\search2.xls");

And if you are creating an new excel file from the scratch, you can do it in this way.

Workbook wb = new Workbook();
//Clears the default sheet Sheet1.
wb.Worksheets.Clear();
//Adding a new worksheet to the Workbook object
wb.Worksheets.Add();
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = wb.Worksheets[0];
//Setting the name of the newly added worksheet
worksheet.Name = "My Worksheet";
//Saving the Excel file
wb.Save("c:\\search2.xls", FileFormatType.Excel2003);
System.Diagnostics.Process.Start("c:\\search2.xls");

We recommend you kindly study different sections in Aspose.Cells documentation specially of Programmer's guide: http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/index.html

Thank you.

Thank You for your reply.I have an empty sheet in the template i removed its fine now.But,Can i have one more problem....

in the code iven below

private void button1_Click(object sender, EventArgs e)

{

Workbook wb = new Workbook();

FileStream fstream = new FileStream("C:\\search1.xls", FileMode.Open);

//Opening the Excel file through the file stream

wb.Open(fstream);

//Adding a new worksheet to the Workbook object

//int i=

int i=wb.Worksheets.Add();

//Obtaining the reference of the newly added worksheet by passing its sheet index

Worksheet worksheet = wb.Worksheets[i];

//Aspose.Cells.Cell cell1 = wb.Worksheets[i].Cells["B5"];

//cell1.PutValue("BINDU");

worksheet.Cells["B5"].PutValue("BINDU");

worksheet.Cells["B6"].PutValue("BINDU");

worksheet.Cells["B7"].PutValue("BINDU");

worksheet.Cells["A11"].PutValue("BINDU");

worksheet.Cells["A12"].PutValue("HIMA");

//Setting the name of the newly added worksheet

//worksheet.Name = "My Worksheet";

//Saving the Excel file

fstream.Close();

wb.Save("C:\\search2.xls", FileFormatType.Excel2003);

System.Diagnostics.Process.Start("C:\\search2.xls");

}

I have added the it to the worksheets as you mentioned now it is not inserting the values (cell.putvalue(string) )into the excel sheet search2.xls.It is only giving the template and one more new sheet sheet2.xls is opened and values are insrted into

that empty sheet.Can you please tell me why this is happening.

Hi,

I am not very sure what do you want to implement, could you elaborate it more. Yes, your code shows that you are addng a new sheet to the "search1.xls" file, inserting your values into it and then saving it as "search2.xls" file.

If you want to put values into the first worksheet of your template "search1.xls" file, you may change your code as follows:

Workbook wb = new Workbook();

FileStream fstream = new FileStream("C:\\search1.xls", FileMode.Open);

//Opening the Excel file through the file stream

wb.Open(fstream);

//Adding a new worksheet to the Workbook object

//int i=

//int i=wb.Worksheets.Add();

//Obtaining the reference of the first worksheet of the excel file by passing its sheet index

Worksheet worksheet = wb.Worksheets[0];

//Aspose.Cells.Cell cell1 = wb.Worksheets[i].Cells["B5"];

//cell1.PutValue("BINDU");

worksheet.Cells["B5"].PutValue("BINDU");

worksheet.Cells["B6"].PutValue("BINDU");

worksheet.Cells["B7"].PutValue("BINDU");

worksheet.Cells["A11"].PutValue("BINDU");

worksheet.Cells["A12"].PutValue("HIMA");

//Setting the name of the newly added worksheet

//worksheet.Name = "My Worksheet";

//Saving the Excel file

fstream.Close();

wb.Save("C:\\search2.xls", FileFormatType.Excel2003);

System.Diagnostics.Process.Start("C:\\search2.xls");

Feel free to contact us anytime if you need further help, we would be happy to help you.

Thank you.

Thank you.I understood what you were saying .Since i am new to aspose.cells i couldnot figure it out properly.

Thank You,

Bindu.