datatable Image导出EXCEL
public void Bind(System.Data.DataTable dt)
{
if (dt == null)
{
return;
}
else
{
#region 操作excel
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
xlWorkBook = new Excel.Application().Workbooks.Add(Type.Missing);
xlWorkBook.Application.Visible = false;
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];
#endregion
//设置标题
int rowIndex = 1; //行
int columnIndex = 0; //列
//将DataTable的列名导入Excel表第一行
foreach (DataColumn dc in dt.Columns)
{
columnIndex++;
xlWorkSheet.Cells[rowIndex, columnIndex] = dc.ColumnName;
//定义标头的宽度
((Excel.Range)xlWorkSheet.Cells[columnIndex, 11]).ColumnWidth = 10;
((Excel.Range)xlWorkSheet.Cells[columnIndex, 12]).ColumnWidth = 10;
((Excel.Range)xlWorkSheet.Cells[columnIndex, 15]).ColumnWidth = 10;
((Excel.Range)xlWorkSheet.Cells[columnIndex, 23]).ColumnWidth = 15;
}
//将DataTable中的数据导入Excel中
for (int i = 0; i < dt.Rows.Count; i++)
{
rowIndex++;
xlWorkSheet.Cells[rowIndex, 1] = dt.Rows[i]["uid"].ToString();
xlWorkSheet.Cells[rowIndex, 2] = dt.Rows[i]["FRID"].ToString();
xlWorkSheet.Cells[rowIndex, 3] = dt.Rows[i]["name"].ToString();
xlWorkSheet.Cells[rowIndex, 4] = dt.Rows[i]["Age"].ToString();
xlWorkSheet.Cells[rowIndex, 5] = dt.Rows[i]["Address"].ToString();
xlWorkSheet.Cells[rowIndex, 6] = dt.Rows[i]["Aliases"].ToString();
xlWorkSheet.Cells[rowIndex, 7] = dt.Rows[i]["CrimeName"].ToString();
xlWorkSheet.Cells[rowIndex, 8] = dt.Rows[i]["Diploma"].ToString();
xlWorkSheet.Cells[rowIndex, 9] = dt.Rows[i]["Nation"].ToString();
xlWorkSheet.Cells[rowIndex, 10] = dt.Rows[i]["ImprisonTerm"].ToString();
xlWorkSheet.Cells[rowIndex, 11] = Convert.ToDateTime(dt.Rows[i]["StartImprison"].ToString()).ToString("yyyy-MM-dd");
xlWorkSheet.Cells[rowIndex, 12] = Convert.ToDateTime(dt.Rows[i]["StopImprison"].ToString()).ToString("yyyy-MM-dd");
xlWorkSheet.Cells[rowIndex, 13] = dt.Rows[i]["PreMetier"].ToString();
xlWorkSheet.Cells[rowIndex, 14] = dt.Rows[i]["Wedlock"].ToString();
xlWorkSheet.Cells[rowIndex, 15] = Convert.ToDateTime(dt.Rows[i]["EnterPrisonTime"].ToString()).ToString("yyyy-MM-dd");
xlWorkSheet.Cells[rowIndex, 16] = dt.Rows[i]["DelictTime"].ToString();
xlWorkSheet.Cells[rowIndex, 17] = dt.Rows[i]["NativePlace"].ToString();
xlWorkSheet.Cells[rowIndex, 18] = dt.Rows[i]["Religion"].ToString();
xlWorkSheet.Cells[rowIndex, 19] = dt.Rows[i]["RewardAndPunish"].ToString();
xlWorkSheet.Cells[rowIndex, 20] = dt.Rows[i]["Commute"].ToString();
xlWorkSheet.Cells[rowIndex, 21] = dt