2017-02-08 21 views
1

我只想爲列之前的excel文檔的標題設置一行。有人能幫我嗎?!按照代碼:如何在生成Excel時在列之前設置行(xlsx)

這裏創建我的Excel格式,列

DataTable dt = new DataTable(); 

dt.Columns.Add("Value 1", typeof(string)); 
dt.Columns.Add("Value 2", typeof(string)); 

這裏後已經定義我的價值(VAR值1 = 「爲例」)

dt.Rows.Add(
value1, 
value2); 

WriteExcelWithNPOI(dt, "xlsx"); 

我希望這是可以理解的。

如有必要也跟隨,使得產生的xlsx代碼:

public void WriteExcelWithNPOI(DataTable dt, String extension){ 

IWorkbook workbook; 

if (extension == "xlsx") 
{ 
    workbook = new XSSFWorkbook(); 
} 
else if (extension == "xls") 
{ 
    workbook = new HSSFWorkbook(); 
} 
else 
{ 
    throw new Exception("This format is not supported"); 
} 

ISheet sheet1 = workbook.CreateSheet("Sheet 1"); 

//make a header row 
IRow row1 = sheet1.CreateRow(0); 

for (int j = 0; j < dt.Columns.Count; j++) 
{ 
    ICell cell = row1.CreateCell(j); 
    String columnName = dt.Columns[j].ToString(); 
    cell.SetCellValue(columnName); 
} 

//loops through data 
for (int i = 0; i < dt.Rows.Count; i++) 
{ 
    IRow row = sheet1.CreateRow(i + 1); 
    for (int j = 0; j < dt.Columns.Count; j++) 
    { 

     ICell cell = row.CreateCell(j); 
     String columnName = dt.Columns[j].ToString(); 
     cell.SetCellValue(dt.Rows[i][columnName].ToString()); 
    } 
} 

using (var exportData = new MemoryStream()) 
{ 
    Response.Clear(); 
    workbook.Write(exportData); 
    if (extension == "xlsx") //xlsx file format 
    { 
     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "Relatorio-Rotas.xlsx")); 
     Response.BinaryWrite(exportData.ToArray()); 
    } 
    else if (extension == "xls") //xls file format 
    { 
     Response.ContentType = "application/vnd.ms-excel"; 
     Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "Relatorio-Rotas.xls")); 
     Response.BinaryWrite(exportData.GetBuffer()); 
    } 
    Response.End(); 
}} 

public void WriteTsv<T>(IEnumerable<T> data, TextWriter output){ 

PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T)); 
foreach (PropertyDescriptor prop in props) 
{ 
    output.Write(prop.DisplayName); // header 
    output.Write("\t"); 
} 
output.WriteLine(); 
foreach (T item in data) 
{ 
    foreach (PropertyDescriptor prop in props) 
    { 
     output.Write(prop.Converter.ConvertToString(
       prop.GetValue(item))); 
     output.Write("\t"); 
    } 
    output.WriteLine(); 
}} 

回答

2

您可以添加前兩列:第一個帶有標題,第二個用一個空格字符(因爲如果設置爲空列名會Column1):

DataTable dt = new DataTable(); 

dt.Columns.Add("Title", typeof(string)); 
dt.Columns.Add(" ", typeof(string)); 

dt.Rows.Add("Value 1", "Value 2"); 
dt.Rows.Add("Example 1", "Example 2"); 

WriteExcelWithNPOI(dt, "xlsx"); 

注意DataTable做沒有colspan屬性,所以如果你想合併列,你必須在你的WriteExcelWithNPOI方法中做到這一點。

1

我又覺得這樣做是寫你的標題信息到Excel文件,關閉它,然後打開它的最簡單的事情將您的實際數據表添加到Excel文件中。 An example implementation can be found in this answer.

請注意,請確保您使用FileStream而不是MemoryStream,以便您的更改將被保存。

相關問題