hi,
am storing string array values in excel sheet.here is the code with this , values are storing at only in 7 th column of excel.
private void Download_Click(object sender, EventArgs e)
{
try
{
Excel.Application xlApp = default(Excel.Application);
Excel.Workbook xlWorkBook = default(Excel.Workbook);
Excel.Worksheet xlWorkSheet = default(Excel.Worksheet);
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "Machine ID";
xlWorkSheet.Cells[1, 2] = "Customer ID";
xlWorkSheet.Cells[1, 3] = "Name";
xlWorkSheet.Cells[1, 4] = "Total Balance";
xlWorkSheet.Cells[1, 5] = "Paid Amount";
xlWorkSheet.Cells[1, 6] = "Last Paid Date";
xlWorkSheet.Cells[1, 7] = "Due Amount";
xlWorkSheet.Cells[1, 1].Interior.ColorIndex = 39;
xlWorkSheet.Cells[1, 2].Interior.ColorIndex = 39;
xlWorkSheet.Cells[1, 3].Interior.ColorIndex = 39;
xlWorkSheet.Cells[1, 4].Interior.ColorIndex = 39;
xlWorkSheet.Cells[1, 5].Interior.ColorIndex = 39;
xlWorkSheet.Cells[1, 6].Interior.ColorIndex = 39;
xlWorkSheet.Cells[1, 7].Interior.ColorIndex = 39;
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
string[] lines = Regex.Split(s, "[$#,]");
String line;
for (int p = 1; p < lines.Length; p++)
{
for (int q = 1; q < 7; q++)
{
line=lines[q];
xlWorkSheet.Cells[p, q] = lines;
if(line!="")
((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[p + 1, q+1 ]).Value2 = lines[p];
}
}
xlWorkBook.SaveAs(fName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
catch (Exception p)
{
MessageBox.Show(p.StackTrace);
}
finally
{
if (xlApp != null)
releaseObject(xlApp);
if (xlWorkBook != null)
releaseObject(xlWorkBook);
if (xlWorkSheet != null)
releaseObject(xlWorkSheet);
}
if (System.IO.File.Exists(fName))
{
if (MessageBox.Show("Would you like to open the excel file?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
System.Diagnostics.Process.Start(fName);
}
catch (Exception ex)
{
MessageBox.Show("Error opening the excel file." + Environment.NewLine +
ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}