C# 如何导出Datatable中的图片列到Excel中,在线等 excelc#图片 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 导出的时候,新建excel。循环Datatable的每一行。新建一个r单元格(range),对不同列的数据进行相应的赋值操作 rowIndex = startRow + head.Rows.Count; foreach (DataRow row in data.Rows) { for (int j = 0; j < columnNames.Length; j++) { if (data.Columns[columnNames[j]] != null) { sheet.Cells[rowIndex, j + 1] = row[columnNames[j]]; } } rowIndex++; }我这不多就是你说的那样导的,图片会出错,类型异常,你导过吗,分享一下代码吧 rowIndex = startRow + head.Rows.Count; foreach (DataRow row in data.Rows) { for (int j = 0; j < columnNames.Length; j++) { if (data.Columns[columnNames[j]] != null) { sheet.Cells[rowIndex, j + 1] = row[columnNames[j]]; } } rowIndex++; } 我这不多就是你说的那样导的,图片会出错,类型异常,你导过吗,分享一下代码吧 这是我昨天做的。你看一下咯。System.Data.DataTable dataTable = dataSet.Tables[0]; int rowNumber = dataTable.Rows.Count; int columnNumber = dataTable.Columns.Count; if (rowNumber == 0) { MessageBox.Show("没有任何数据可以导入到Excel文件!"); return false; } //建立Excel对象 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); Workbook xlbook = excel.Application.Workbooks.Add(true); Worksheet xlsheet = (Worksheet)xlbook.Worksheets[1]; xlsheet.Name = "库存报表"; //表头样式 excel.ActiveSheet.Rows[2].Font.Name = "隶书"; excel.ActiveSheet.Rows[2].Font.Bold = true; excel.ActiveSheet.Rows[2].Font.Size = 20; //excel.ActiveSheet.Rows[2].Font.UnderLine = true; //列名样式 excel.ActiveSheet.Rows[7].Font.Name = "隶书"; excel.ActiveSheet.Rows[7].Font.Bold = true; //excel.ActiveSheet.Rows[7].Font.UnderLine = true; int colIndex = 0; int RowIndex = 7; excel.Cells[2, 7] = "深圳市********有限公司"; excel.Cells[4, 1] = "客户名称:"; excel.Cells[4, 3] = this.UCSelectCoagent.Text; excel.Cells[4, 8] = "所在仓库:"; excel.Cells[4, 10] = this.CMBWareAddress.Text; excel.Cells[5, 1] = "库存截止时间:"; excel.Cells[5, 3] = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day; excel.Cells[5, 8] = "报表生成时间:"; excel.Cells[5, 10] = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day; //开始写入每列的标题 foreach (DataColumn dc in dataTable.Columns) { colIndex++; excel.Cells[RowIndex, colIndex] = dc.Caption; } //填充数据 for (int c = 0; c < rowNumber; c++) { for (int j = 0; j < columnNumber; j++) { excel.Cells[RowIndex + 1, j + 1] = dataTable.Rows[c].ItemArray[j]; } RowIndex++; } Microsoft.Office.Interop.Excel.Range range5_7 = xlsheet.Range[xlsheet.Cells[7, 1], xlsheet.Cells[RowIndex, columnNumber]]; //内外边框 range5_7.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous; range5_7.Borders.Weight = 2; string sampleFile = string.Format("库存报表{0}.xlsx", DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day); SaveFileDialog sd = new SaveFileDialog(); sd.FileName = sampleFile; sd.Filter = "文档类型(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件(*.xls;*.xlsx)|*.xls;*.xlsx"; if (DialogResult.OK == sd.ShowDialog()) { string FileNames = sd.FileName.ToString(); xlbook.SaveCopyAs(FileNames); } 我的这个方法你可以直接复制过去用。在一个按钮的单击事件调用我那个方法就行。然后看效果去修改成你想要的请教7楼,你确定这个可以导出Datatable中的图片列(Byte[]),我这边报错的。我原来的写法是这样的 sheet.Cells[rowIndex, j + 1] = row[columnNames[j]]; 跟你的差不多 datagrid中的数据直接保存到 ms sql 的一个表中 [攒分贴] 用SqlDependency类为SQL2005查询提供更改通知 C#读取excel数据问题 关于FileStream的Seek方法与BinaryReader 大家发个能下visual studio.net 2003的地址吧(CSDN里面的地址好像都不行啊) treeview树刷新问题 微信接入实现问题 思归大哥:请帮我看看这段代码.谢谢! 在使用.NET技术开发的时候(C#语言),我如何判断我的程序连接上了数据库? 请教如何用C#取网卡MAC的地址?? dotmsn问题求助,主动发送消息 基于主机的入侵检测系统!!!!
循环Datatable的每一行。新建一个r单元格(range),对不同列的数据进行相应的赋值操作
foreach (DataRow row in data.Rows)
{
for (int j = 0; j < columnNames.Length; j++)
{
if (data.Columns[columnNames[j]] != null)
{
sheet.Cells[rowIndex, j + 1] = row[columnNames[j]];
}
}
rowIndex++;
}
我这不多就是你说的那样导的,图片会出错,类型异常,你导过吗,分享一下代码吧
rowIndex = startRow + head.Rows.Count;
foreach (DataRow row in data.Rows)
{
for (int j = 0; j < columnNames.Length; j++)
{
if (data.Columns[columnNames[j]] != null)
{
sheet.Cells[rowIndex, j + 1] = row[columnNames[j]];
}
}
rowIndex++;
}
我这不多就是你说的那样导的,图片会出错,类型异常,你导过吗,分享一下代码吧
System.Data.DataTable dataTable = dataSet.Tables[0];
int rowNumber = dataTable.Rows.Count;
int columnNumber = dataTable.Columns.Count;
if (rowNumber == 0)
{
MessageBox.Show("没有任何数据可以导入到Excel文件!");
return false;
} //建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Workbook xlbook = excel.Application.Workbooks.Add(true);
Worksheet xlsheet = (Worksheet)xlbook.Worksheets[1];
xlsheet.Name = "库存报表"; //表头样式
excel.ActiveSheet.Rows[2].Font.Name = "隶书";
excel.ActiveSheet.Rows[2].Font.Bold = true;
excel.ActiveSheet.Rows[2].Font.Size = 20;
//excel.ActiveSheet.Rows[2].Font.UnderLine = true; //列名样式
excel.ActiveSheet.Rows[7].Font.Name = "隶书";
excel.ActiveSheet.Rows[7].Font.Bold = true;
//excel.ActiveSheet.Rows[7].Font.UnderLine = true; int colIndex = 0;
int RowIndex = 7; excel.Cells[2, 7] = "深圳市********有限公司";
excel.Cells[4, 1] = "客户名称:";
excel.Cells[4, 3] = this.UCSelectCoagent.Text;
excel.Cells[4, 8] = "所在仓库:";
excel.Cells[4, 10] = this.CMBWareAddress.Text; excel.Cells[5, 1] = "库存截止时间:";
excel.Cells[5, 3] = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;
excel.Cells[5, 8] = "报表生成时间:";
excel.Cells[5, 10] = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day;
//开始写入每列的标题
foreach (DataColumn dc in dataTable.Columns)
{
colIndex++;
excel.Cells[RowIndex, colIndex] = dc.Caption;
}
//填充数据
for (int c = 0; c < rowNumber; c++)
{
for (int j = 0; j < columnNumber; j++)
{
excel.Cells[RowIndex + 1, j + 1] = dataTable.Rows[c].ItemArray[j];
}
RowIndex++;
}
Microsoft.Office.Interop.Excel.Range range5_7 = xlsheet.Range[xlsheet.Cells[7, 1], xlsheet.Cells[RowIndex, columnNumber]];
//内外边框
range5_7.Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range5_7.Borders.Weight = 2; string sampleFile = string.Format("库存报表{0}.xlsx", DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day);
SaveFileDialog sd = new SaveFileDialog();
sd.FileName = sampleFile;
sd.Filter = "文档类型(*.xls;*.xlsx)|*.xls;*.xlsx|所有文件(*.xls;*.xlsx)|*.xls;*.xlsx";
if (DialogResult.OK == sd.ShowDialog())
{
string FileNames = sd.FileName.ToString();
xlbook.SaveCopyAs(FileNames);
}