private void writeDataToExcelFile(
DataSet ds)
{
var workbook = new HSSFWorkbook();
String sheetName = "Orignal";
string filename = Convert.ToString(ds.Tables[0].Rows[0]["filename"]);
for (int i = 0; i < ds.Tables.Count; i++)
{
DataTable dt = ds.Tables[i];
if (dt.Rows.Count > 0)
{
if (i == 0)
sheetName = "Original";
else sheetName = "plagiarism";
var Sheet = workbook.CreateSheet(sheetName);
int cols = 0;
var myRow = Sheet.CreateRow(0);
var style1 = workbook.CreateCellStyle();
// cell background
style1.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.VIOLET.index;
// font color
var font1 = workbook.CreateFont();
font1.Color = NPOI.HSSF.Util.HSSFColor.BLUE.index;
style1.SetFont(font1);
foreach (DataColumn column in dt.Columns)
{
var Cell = myRow.CreateCell(cols);
Cell.SetCellValue(column.ColumnName);
Cell.CellStyle = style1;
cols++;
}
for (int rowNum = 0; rowNum < dt.Rows.Count; rowNum++)
{
myRow = Sheet.CreateRow(rowNum + 1);
for (int cellNum = 0; cellNum < dt.Columns.Count; cellNum++)
{
var Cell = myRow.CreateCell(cellNum);
Cell.SetCellValue(Convert.ToString(dt.Rows[rowNum][cellNum]));
}
}
}
}
using (var buffer = new MemoryStream())
{
workbook.Write(buffer);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "test.xls"));
Response.Clear();
Response.BinaryWrite(buffer.GetBuffer());
Response.End();
}
}
DataSet ds)
{
var workbook = new HSSFWorkbook();
String sheetName = "Orignal";
string filename = Convert.ToString(ds.Tables[0].Rows[0]["filename"]);
for (int i = 0; i < ds.Tables.Count; i++)
{
DataTable dt = ds.Tables[i];
if (dt.Rows.Count > 0)
{
if (i == 0)
sheetName = "Original";
else sheetName = "plagiarism";
var Sheet = workbook.CreateSheet(sheetName);
int cols = 0;
var myRow = Sheet.CreateRow(0);
var style1 = workbook.CreateCellStyle();
// cell background
style1.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.VIOLET.index;
// font color
var font1 = workbook.CreateFont();
font1.Color = NPOI.HSSF.Util.HSSFColor.BLUE.index;
style1.SetFont(font1);
foreach (DataColumn column in dt.Columns)
{
var Cell = myRow.CreateCell(cols);
Cell.SetCellValue(column.ColumnName);
Cell.CellStyle = style1;
cols++;
}
for (int rowNum = 0; rowNum < dt.Rows.Count; rowNum++)
{
myRow = Sheet.CreateRow(rowNum + 1);
for (int cellNum = 0; cellNum < dt.Columns.Count; cellNum++)
{
var Cell = myRow.CreateCell(cellNum);
Cell.SetCellValue(Convert.ToString(dt.Rows[rowNum][cellNum]));
}
}
}
}
using (var buffer = new MemoryStream())
{
workbook.Write(buffer);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "test.xls"));
Response.Clear();
Response.BinaryWrite(buffer.GetBuffer());
Response.End();
}
}
No comments:
Post a Comment