/// <summary>
/// datatable导出到excel
/// </summary>
/// <param name="dtSource">datatable</param>
/// <param name="strFileName">strFileName</param>
/// <param name="sup">Supplier</param>
/// <returns></returns>
public static bool ExportEasy(DataTable dtSource, string strFileName, Supplier sup)
{
try
{
HSSFWorkbook workbook = new HSSFWorkbook();
//HSSFSheet sheet = workbook.CreateSheet();
ISheet sheet = workbook.CreateSheet();
//填充表头
//HSSFRow dataRow = sheet.CreateRow(0);
IRow dataRow = sheet.CreateRow(0);
//foreach (DataColumn column in dtSource.Columns)
//{
// dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
//}
//合并单元格
dataRow.CreateCell(2).SetCellValue(sup.SupplierCode);
SetCellRangeAddress(sheet, 0, 0, 0, 1);
dataRow.CreateCell(0).SetCellValue("供应商代码:");
SetCellRangeAddress(sheet, 0, 0, 3, 4);
dataRow.CreateCell(3).SetCellValue("供应商名称:");
SetCellRangeAddress(sheet, 0, 0, 5, 7);
dataRow.CreateCell(8).SetCellValue("专柜编码:");
SetCellRangeAddress(sheet, 0, 0, 21, 22);
dataRow.CreateCell(21).SetCellValue("经营方式:");
dataRow.CreateCell(5).SetCellValue(sup.SupplierName);
dataRow.CreateCell(9).SetCellValue(sup.SpecialCode);
dataRow.CreateCell(23).SetCellValue(sup.ModeOperation);
//填充内容
for (int i = 1; i < dtSource.Rows.Count; i++)
{
dataRow = sheet.CreateRow(i + 1);
for (int j = 1; j < dtSource.Columns.Count; j++)
{
if (j > 19)
{
if (j == 20)
continue;
else
{
if (j > 23)
{
if (j == 24)
continue;
else
{
dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i - 1][j - 2].ToString());
}
}
else
{
dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i - 1][j - 1].ToString());
}
}
}
else
{
dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i - 1][j].ToString());
}
}
}
//保存
using (MemoryStream ms = new MemoryStream())
{
using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
{
workbook.Write(fs);
}
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 合并单元格
/// </summary>
/// <param name="sheet">要合并单元格所在的sheet</param>
/// <param name="rowstart">开始行的索引</param>
/// <param name="rowend">结束行的索引</param>
/// <param name="colstart">开始列的索引</param>
/// <param name="colend">结束列的索引</param>
public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
{
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
sheet.AddMergedRegion(cellRangeAddress);
}
/// datatable导出到excel
/// </summary>
/// <param name="dtSource">datatable</param>
/// <param name="strFileName">strFileName</param>
/// <param name="sup">Supplier</param>
/// <returns></returns>
public static bool ExportEasy(DataTable dtSource, string strFileName, Supplier sup)
{
try
{
HSSFWorkbook workbook = new HSSFWorkbook();
//HSSFSheet sheet = workbook.CreateSheet();
ISheet sheet = workbook.CreateSheet();
//填充表头
//HSSFRow dataRow = sheet.CreateRow(0);
IRow dataRow = sheet.CreateRow(0);
//foreach (DataColumn column in dtSource.Columns)
//{
// dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
//}
//合并单元格
dataRow.CreateCell(2).SetCellValue(sup.SupplierCode);
SetCellRangeAddress(sheet, 0, 0, 0, 1);
dataRow.CreateCell(0).SetCellValue("供应商代码:");
SetCellRangeAddress(sheet, 0, 0, 3, 4);
dataRow.CreateCell(3).SetCellValue("供应商名称:");
SetCellRangeAddress(sheet, 0, 0, 5, 7);
dataRow.CreateCell(8).SetCellValue("专柜编码:");
SetCellRangeAddress(sheet, 0, 0, 21, 22);
dataRow.CreateCell(21).SetCellValue("经营方式:");
dataRow.CreateCell(5).SetCellValue(sup.SupplierName);
dataRow.CreateCell(9).SetCellValue(sup.SpecialCode);
dataRow.CreateCell(23).SetCellValue(sup.ModeOperation);
//填充内容
for (int i = 1; i < dtSource.Rows.Count; i++)
{
dataRow = sheet.CreateRow(i + 1);
for (int j = 1; j < dtSource.Columns.Count; j++)
{
if (j > 19)
{
if (j == 20)
continue;
else
{
if (j > 23)
{
if (j == 24)
continue;
else
{
dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i - 1][j - 2].ToString());
}
}
else
{
dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i - 1][j - 1].ToString());
}
}
}
else
{
dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i - 1][j].ToString());
}
}
}
//保存
using (MemoryStream ms = new MemoryStream())
{
using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
{
workbook.Write(fs);
}
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 合并单元格
/// </summary>
/// <param name="sheet">要合并单元格所在的sheet</param>
/// <param name="rowstart">开始行的索引</param>
/// <param name="rowend">结束行的索引</param>
/// <param name="colstart">开始列的索引</param>
/// <param name="colend">结束列的索引</param>
public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
{
CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
sheet.AddMergedRegion(cellRangeAddress);
}