-1

I want to save the Datagridview as excel file in another computer in Network drive using LAN. I am using the following code:

xlWorkBook.SaveAs("\\User2\\share\\d.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

Here,User2 is another computer in Network Drive...I want to save my file to user2 computer in Network drive.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
ASKA
  • 21
  • 1
  • 6
  • This is a duplicate of: http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp – Paul Mar 04 '15 at 07:21

1 Answers1

0

You can use excellibrary which is open source on Google Code.

//create new xls file
string file = "\\\\server\\share\\newdoc.xls";
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");
worksheet.Cells[0, 1] = new Cell((short)1);
worksheet.Cells[2, 0] = new Cell(9999999);
worksheet.Cells[3, 3] = new Cell((decimal)3.45);
worksheet.Cells[2, 2] = new Cell("Text string");
worksheet.Cells[2, 4] = new Cell("Second string");
worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00");
worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY\-MM\-DD");
worksheet.Cells.ColumnWidth[0, 1] = 3000;
workbook.Worksheets.Add(worksheet);
workbook.Save(file);

// open xls file
Workbook book = Workbook.Load(file);
Worksheet sheet = book.Worksheets[0];

 // traverse cells
 foreach (Pair<Pair<int, int>, Cell> cell in sheet.Cells)
 {
     dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;
 }

 // traverse rows by Index
 for (int rowIndex = sheet.Cells.FirstRowIndex; 
        rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
 {
     Row row = sheet.Cells.GetRow(rowIndex);
     for (int colIndex = row.FirstColIndex; 
        colIndex <= row.LastColIndex; colIndex++)
     {
         Cell cell = row.GetCell(colIndex);
     }
 }

For a fuller answer, and additional libraries, you can read Create Excel (.XLS and .XLSX) file from C#. Answer is posted to community wiki, so it should stick around a while

Community
  • 1
  • 1
Paul
  • 1,011
  • 10
  • 13