Skip to content

Column Width Excel file

TheOne

Asked Mar 5, 2012, 11:55 PM EST2 answers
Share
LinkedInX
Hi,
 
I created an Excel file from a datatable but i have a problem to resize column, this is the first problem. The second is how can i write in a specific cell(for example the cell(1,2) I need this for the header for example).Can you help me?.This is my code

  DataTable dt = (DataTable) ViewState["CurrentTable"];
 
  Workbook book = new Workbook();
  WorksheetStyle style = book.Styles.Add("Entete");
  style.Font.Bold = true;
  style.Font.Color = "White";
  style.Alignment.Horizontal = StyleHorizontalAlignment.Center;
  style.Interior.Color = "Blue";
  style.Interior.Pattern = StyleInteriorPattern.Solid;
 
  //create the first sheet
  Worksheet sheet = book.Worksheets.Add("Detail Seance");
 
  //The second sheet
  Worksheet sheetConf = book.Worksheets.Add("Total");
 
 
  //The header
  WorksheetRow row = sheet.Table.Rows.Add();
  row.Cells.Add(new WorksheetCell("Historique Exo"){ MergeAcross = 5 });
 
  row = sheet.Table.Rows.Add();
  Excel.Range range;
 
  foreach (DataColumn dc in dt.Columns)
  {
  row.Cells.Add(new WorksheetCell(dc.ColumnName, DataType.String, "Entete"));
  }
 
  int j;
  foreach (DataRow dr in dt.Rows)
  {
  row = sheet.Table.Rows.Add();
  for (j = 0; j < dt.Columns.Count; j++)
  {
  row.Cells.Add(new WorksheetCell(dr[j].ToString()));
  //I want for example to specify the width of the cell added
  }
  }
 
  //save Excel file
  book.Save(Response.OutputStream);


I hope that you can help

tahnks

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I found that i can resize all width column, look code above but  i want to initialize some width column(because in some column i will have for example 4 character and other have 15 caracter it depends on the data inserted in)

    sheet.Table.DefaultColumnWidth = 120;
    0
  • 0

    This might help you.

    assuming that you alreadt have a workbook "wkBook"

    Java
                        wkSheet = (Worksheet)wkBook.Sheets[1];
                        // single column width adjust
                        Range aRangeSingle = wkSheet.get_Range("A1", "A1");
                        aRangeSingle.ColumnWidth = 50;
                        // multi-column width adjust
                        Range aRangeMultiColumn = wkSheet.get_Range("C1", "G1");
                        aRangeMultiColumn.ColumnWidth = 4;
                        //single cell writing values
                        aRangeSingle.Value2 = "Solotion #1: single cell value";
                        wkSheet.Cells[3, 2] = "Solotion #2: single cell value";
                        //multi-cell writing values
                        aRangeMultiColumn.Value2 = "Multi-cell writing";

    ==========Don't forget to tick the correct answer==========

    0