file in excel file format by using SteamWriter and Stream
Reader.it's is easy to make an excel document using the
StreamWriter class above.the example shown below-
An excel file with customer details.
String[] custId ={ "01", "02" };
String[] custName ={ "mridul", "you" };
String[] custAddress ={ "ka/3,3rd street", "26,north Road" };
StreamWriter streamw = new StreamWriter("thefile.xls", false,
Encoding.ASCII);
streamw.WriteLine("CustID"+"\t"+"customer Name" +"\t"+
"cust Address" );
for (int j = 0; j < id.Length; j++)
streamw.WriteLine(custId[j] +"\t"+ custName[j] +"\t"+
custAddress[j]);streamw.Close();
in the above program we created
String Array for Customer id details,Customer name details,
Customer address details.we created stream writer object.In
this object we passed encoding type-Encoding.ASCII.then wrote
Headers of the table(Column Heading of the Report/statistics).
we wrote data into the file.Here the tab separated every cells.
you will see the output in a excel file if you run the program.
but after that if you try to save then you may get a message-
"Do you want to keep the workbook in this format?" becuse we
seperated our columns by using tab.you should choose 'yes'.
how to read an excel file?
we can read the above excel file using StreamReader.but it's
happens if we kept [tab delimited] format.using same tab
we just read line and divide data.then we handle those data.
a example follows-
StreamReader streamr = new StreamReader(file);
int counter = 0;int rows = 0;
while (!streamr.EndOfStream){
String csharp = streamr.ReadLine(); rows++;}
streamr.Close();
streamr = new StreamReader(file);
String[] customerId = new String[rows];
String[] customerName = new String[rows];
String[] customerAddress = new String[rows];
while (!streamr.EndOfStream){
String csharp = streamr.ReadLine();
String[] column = csharp.Split('\t');
customerId[counter] = column[0];
customerName[counter] = column[1];
customerAddress[counter] = column[2];
counter++;} sreamr.Close();