如何批量提取数码图片的exif信息,导入到EXCEL表格中
如题,怎么把一个文件夹内的所有图片信息导成一个excel文件,想破头了
WinForm Exif Excel
--------------------编程问答--------------------
来个人帮忙看看啊
--------------------编程问答--------------------
提取一个文件的EXIF实现了吗?
--------------------编程问答--------------------
提取文件的exif实现了,就是批量导入没有
--------------------编程问答--------------------
除
--------------------编程问答--------------------
来个人,帮帮忙呗
--------------------编程问答--------------------
批量导入不是写到excel中么?
--------------------编程问答--------------------
是写到excel中啊,我的代码写好了,就是可以导入一条数据到excel,但我不知道怎么批量导入,等下帖代码,大哥哥大姐姐们可以帮忙看下
--------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace GetphotoInfo
{
public partial class Form1 : Form
{
String pt;//选择的某一张图片
string DataInfo;//读取到的照片信息
FolderBrowserDialog dialog;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 选择图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBrowse_Click(object sender, EventArgs e)
{
dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件路径";
if (dialog.ShowDialog() == DialogResult.OK)
{
GetPic(dialog.SelectedPath);
}
txtFileName.Text = dialog.SelectedPath;
//pictureBoxThumbnail.ImageLocation = dialog.SelectedPath;
//btnPopulate_Click(sender, e);
}
/// <summary>
/// 加载文件夹内的图片
/// </summary>
/// <param name="path"></param>
private void GetPic(string path)
{
String File = path;
String[] Files = Directory.GetFiles(File);
foreach (string file in Files)
{
listBox1.Items.Add(file);
}
}
private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
pt = (String)listBox1.SelectedItem;
this.pictureBoxThumbnail.Image = System.Drawing.Image.FromFile((String)pt);
ReadPhoto();
}
/// <summary>
/// 显示数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_RenderData_Click(object sender, EventArgs e)
{
ReadPhoto();
}
private void ReadPhoto()
{
ExifReader reader = null;
try
{
reader = new ExifReader(pt);
string props = "";
foreach (ushort tagID in Enum.GetValues(typeof(ExifTags)))
{
object val;
if (reader.GetTagValue(tagID, out val))
{
string renderedTag;
if (val is Array)
{
var array = (Array)val;
renderedTag = "";
if (array.Length > 0)
{
foreach (object item in array)
renderedTag += item + ",";
renderedTag = renderedTag.Substring(0, renderedTag.Length - 1);
}
}
else
renderedTag = val.ToString();
string NameTag = "";
}
props += string.Format("{0}:{1} \r\n", NameTag, renderedTag);
}
}
props = props.Substring(0, props.Length - 2);
DataInfo += props;
// txtFields.Text = props;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (reader != null)
reader.Dispose();
}
}
/// <summary>
/// 导出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_outdata_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "xls"; //设置默认扩展名为xls
sfd.Filter = "Excel文件(*.xls)|*.xls";//另存文件时文件类型框中出现的内容
if (sfd.ShowDialog() == DialogResult.OK) //获取选定的另存文件对话框存在
{
DoExport(pt,sfd.FileName);
}
}
//导出到Excel中
private void DoExport(string path ,string fielName)
{
string[] b = new string[100];
StringBuilder sb = new StringBuilder();
Dictionary<string, string> dic = new Dictionary<string, string>();
string[] str = DataInfo.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < str.Length; i++)
{
if (str[i].ToString() == "")
{
continue;
}
dic.Add(str[i].Substring(0, (str[i].IndexOf(':'))), str[i].Substring((str[i].IndexOf(':') + 1), str[i].Length - str[i].IndexOf(':') - 1));
}
DataTable dt = new DataTable();
DataSet ds = new DataSet();
DataRow dr = dt.NewRow();
dt.Columns.Add("文件名");
dr["文件名"] = Path.GetFileName(path);
//string[] pathName = new string[listBox1.Items.Count];
//for (int i = 0; i < listBox1.Items.Count; i++)
//{
// dr = dt.NewRow();
// pathName[i] = Path.GetFileName(listBox1.Items[i].ToString());
// dr["文件名"] = pathName[i];
// dt.Rows.Add(dr);
//}
foreach (var item in dic)
{
dt.Columns.Add(item.Key, Type.GetType("System.String"));
dr[item.Key] = item.Value;
}
dt.Rows.InsertAt(dr, 0);
ds.Tables.Add(dt);
dataGridView1.DataSource = dt;
//导出到execl
try
{
//没有数据的话就不往下执行
if (dataGridView1.Rows.Count == 0)
return;
//实例化一个Excel.Application对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写
excel.Visible = false;
//新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错
Microsoft.Office.Interop.Excel.Workbook xlBook = excel.Workbooks.Add(true);
//生成Excel中列头名称
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
}
//把DataGridView当前页的数据保存在Excel中
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
}
}
}
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.ActiveSheet;
//得到 Range 范围 域对象
Microsoft.Office.Interop.Excel.Range range = sheet.get_Range("A1", "W69");
//设置Excel表格的 列宽
//sheet.get_Range("A1", "Z1").ColumnWidth = 15;
sheet.Cells.Columns.AutoFit();
range.HorizontalAlignment = HorizontalAlignment.Right;
//range.Font.Size = 10;
//range.Borders.LineStyle = 1;
//excel.DisplayAlerts = true;
//excel.SheetsInNewWorkbook = 1;
//设置禁止弹出保存和覆盖的询问提示框
//excel.DisplayAlerts = false;
// excel.AlertBeforeOverwriting = false;
//保存工作簿
// excel.Application.Workbooks.Add(true).Save();
//保存excel文件
xlBook.SaveAs(fielName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//确保Excel进程关闭
excel.Quit();
excel = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示");
}
}
}
}
--------------------编程问答--------------------
没人吗,不要沉啊
--------------------编程问答--------------------
你的 ReadPhoto方法怎么括号都没有配对啊?
--------------------编程问答--------------------
有可能是复制代码的时候弄错了,这个我已经解决,感谢大家,等下帖代码
--------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace GetphotoInfo
{
public partial class Form1 : Form
{
DataTable dt = new DataTable();
int count = 0;//插入到datatable中的位置
int number = 0;//dr["文件名"]列是否已经存在
int number1 = 0;//datatable中的列名是否已经存在
string DataInfo;//读取到的照片信息
FolderBrowserDialog dialog;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 选择图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBrowse_Click(object sender, EventArgs e)
{
dialog = new FolderBrowserDialog();
dialog.Description = "请选择文件路径";
if (dialog.ShowDialog() == DialogResult.OK)
{
GetPic(dialog.SelectedPath);
}
txtFileName.Text = dialog.SelectedPath;
pictureBoxThumbnail.ImageLocation = dialog.SelectedPath;
//btnPopulate_Click(sender, e);
}
/// <summary>
/// 加载文件夹内的图片
/// </summary>
/// <param name="path"></param>
private void GetPic(string path)
{
String File = path;
String[] Files = Directory.GetFiles(File);
foreach (string file in Files)
{
ReadPhoto(file);
ShowRowData(file);
dgv_Data.DataSource = dt;
listBox1.Items.Add(file);
}
}
private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
//String pt = (String)listBox1.SelectedItem;
//this.pictureBoxThumbnail.Image = System.Drawing.Image.FromFile((String)pt);
// ReadPhoto();
}
/// <summary>
/// 读取一张照片信息
/// </summary>
/// <param name="selected_PhotoPath">图片路径</param>
private void ReadPhoto(string selected_PhotoPath)
{
ExifReader reader = null;
try
{
reader = new ExifReader(selected_PhotoPath);
string props = "";
foreach (ushort tagID in Enum.GetValues(typeof(ExifTags)))
{
object val;
if (reader.GetTagValue(tagID, out val))
{
string renderedTag;
if (val is Array)
{
var array = (Array)val;
renderedTag = "";
if (array.Length > 0)
{
foreach (object item in array)
renderedTag += item + ",";
renderedTag = renderedTag.Substring(0, renderedTag.Length - 1);
}
}
else
renderedTag = val.ToString();
}
props += string.Format("{0}:{1} \r\n", NameTag, renderedTag);
}
}
props = props.Substring(0, props.Length - 2);
DataInfo = props;
// txtFields.Text = props;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (reader != null)
reader.Dispose();
}
}
/// <summary>
/// 导出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_outdata_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "xls"; //设置默认扩展名为xls
sfd.Filter = "Excel文件(*.xls)|*.xls";//另存文件时文件类型框中出现的内容
if (sfd.ShowDialog() == DialogResult.OK) //获取选定的另存文件对话框存在
{
DoExport(sfd.FileName);
}
}
/// <summary>
/// 显示一张图片的信息
/// </summary>
/// <param name="path">图片的路径</param>
/// <returns></returns>
private DataTable ShowRowData(string path)
{
// string path = txtFileName.Text;
// DataSet ds = new DataSet();
DataRow dr = dt.NewRow();
if (number == 0)
{
dt.Columns.Add("文件名");
}
number++;
dr["文件名"] = Path.GetFileName(path);
Dictionary<string, string> dic = new Dictionary<string, string>();
string[] str = DataInfo.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < str.Length; i++)
{
if (str[i].ToString() == "")
{
continue;
}
//if (!dic.ContainsKey(str[i].Substring(0, (str[i].IndexOf(':')))))
// dic.Add(str[i].Substring(0, (str[i].IndexOf(':'))),
// str[i].Substring((str[i].IndexOf(':') + 1),
// str[i].Length - str[i].IndexOf(':') - 1));
if (!dic.ContainsKey(str[i].Substring(0, (str[i].IndexOf(':')))))
{
dic.Add(str[i].Substring(0, (str[i].IndexOf(':'))), str[i].Substring((str[i].IndexOf(':') + 1), str[i].Length - str[i].IndexOf(':') - 1));
}
}
foreach (var item in dic)
{
if (number1 == 0)
{
dt.Columns.Add(item.Key, Type.GetType("System.String"));
}
dr[item.Key] = item.Value;
}
dt.Rows.InsertAt(dr, count);
number1++;
count++;
// ds.Tables.Add(dt);
// dgv_Data.Rows.Add(new DataGridView(str));
return dt;
}
/// <summary>
/// 导出到Excel中
/// </summary>
/// <param name="fielName">保存为Excel的文件名称</param>
private void DoExport(string fielName)
{
//导出到execl
try
{
//没有数据的话就不往下执行
if (dgv_Data.Rows.Count == 0)
return;
//实例化一个Excel.Application对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
//让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写
excel.Visible = false;
//新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错
Microsoft.Office.Interop.Excel.Workbook xlBook = excel.Workbooks.Add(true);
//生成Excel中列头名称
for (int i = 0; i < dgv_Data.Columns.Count; i++)
{
excel.Cells[1, i + 1] = dgv_Data.Columns[i].HeaderText;
}
//把DataGridView当前页的数据保存在Excel中
for (int i = 0; i < dgv_Data.Rows.Count - 1; i++)
{
for (int j = 0; j < dgv_Data.Columns.Count; j++)
{
if (dgv_Data[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = dgv_Data[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dgv_Data[j, i].Value.ToString();
}
}
}
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.ActiveSheet;
//得到 Range 范围 域对象
Microsoft.Office.Interop.Excel.Range range = sheet.get_Range("A1", "W69");
//设置Excel表格的 列宽
//sheet.get_Range("A1", "Z1").ColumnWidth = 15;
//sheet.Cells.Columns.AutoFit();
range.HorizontalAlignment = HorizontalAlignment.Right;
//range.Font.Size = 10;
//range.Borders.LineStyle = 1;
//excel.DisplayAlerts = true;
//excel.SheetsInNewWorkbook = 1;
//设置禁止弹出保存和覆盖的询问提示框
//excel.DisplayAlerts = false;
// excel.AlertBeforeOverwriting = false;
//保存工作簿
// excel.Application.Workbooks.Add(true).Save();
//保存excel文件
xlBook.SaveAs(fielName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//确保Excel进程关闭
excel.Quit();
excel = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示");
}
}
}
}
--------------------编程问答--------------------
考虑变成XML文件,再导入
--------------------编程问答--------------------
除
补充:.NET技术 , C#