如何将边框应用于导出的文件?

时间:2016-06-03 10:14:50

标签: c# export-to-excel

我正在使用此代码导出excel文件。它工作正常。

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

    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            Table table = new Table();
            TableRow row = new TableRow();
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
            foreach (PropertyDescriptor prop in props)
            {
                TableCell cell = new TableCell();
                cell.Text = prop.Name;
                row.Cells.Add(cell);
            }

            table.Rows.Add(row);


            foreach (T item in data)
            {
                row = new TableRow();
                foreach (PropertyDescriptor prop in props)
                {
                    TableCell cell = new TableCell();
                    cell.Text = prop.Converter.ConvertToString(prop.GetValue(item));

                    row.Cells.Add(cell);

                }

                table.Rows.Add(row);
            }

            table.RenderControl(htw);  
            output.Write(sw.ToString());
        }
    }

}

并给我这个输出 enter image description here

但输出哪个 我想要的是enter image description here

我想要所有行和列的边框。我需要在代码中进行哪些更改? 提前谢谢......

1 个答案:

答案 0 :(得分:0)

这可能会对你有所帮助。虽然我没有测试过代码,但你可以做类似

的事情
//Add Borders for All Cells
            sheet.Range["A1:E9"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
            sheet.Range["A1:E9"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
            sheet.Range["A1:E9"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
            sheet.Range["A1:E9"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;

注意:参考https://janewdaisy.wordpress.com/2011/12/05/c-add-borders-for-cells/