ASP.net读取Excel文件的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.net读取Excel文件的问题相关的知识,希望对你有一定的参考价值。
如何读取此Excel中sheet1工作薄中表格的数据??
之前写过读取Excel的Code,不过那个首行就是字段名称,剩下的行则是数据行了,而这个,则不同,咋办???
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace ExcelConvertor
public partial class Form1 : Form
public Form1()
InitializeComponent();
private void button1_Click(object sender, EventArgs e)
this.openFileDialog1.Multiselect = true;
this.openFileDialog1.Filter = "EXCEL文件(*.xls)|*.xls";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
this.textBox1.Text = openFileDialog1.FileName;
filename = openFileDialog1.FileName;
private void button2_Click(object sender, EventArgs e)
System.Data.DataTable dt = new System.Data.DataTable();
ExcelToDT(this.textBox1.Text, ref dt);
ExcelConvertor(dt);
public static string filename;
public static bool ExcelConvertor(System.Data.DataTable dt)
string s = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
string savefilepath = filename.Substring(0, filename.LastIndexOf('\\') + 1) +s+ ".dat";
string djbh = dt.Rows[2][0].ToString(); //单据编号
string lybm = dt.Rows[2][2].ToString(); //领用部门
string flbm = dt.Rows[2][3].ToString(); //发料部门
string lyrq = "";//领用日期
if (dt.Rows[2][4].ToString() != "")
if (dt.Rows[2][4].ToString().Contains("-"))
lyrq = dt.Rows[2][4].ToString();//领用日期
else
lyrq = DateTime.FromOADate(Convert.ToInt32(dt.Rows[2][4].ToString())).ToString("d"); //领用日期
else
lyrq = DateTime.Now.ToLongDateString();
string year = DateTime.Parse(lyrq).Year.ToString();
string month = DateTime.Parse(lyrq).Month.ToString();
string xmgd = dt.Rows[4][6].ToString();//项目/工段
string cpdm = dt.Rows[4][0].ToString();//产品代码
string cpmc = dt.Rows[4][2].ToString();//产品名称
string pcs = dt.Rows[4][4].ToString();//排产计划数
// int rowcount = dt.Rows.Count;
//生成Excel文件
Microsoft.Office.Interop.Excel.Application app =
new Microsoft.Office.Interop.Excel.ApplicationClass();
try
app.Visible = false;
Workbook wBook = app.Workbooks.Add(true);
Worksheet wSheet = wBook.Worksheets[1] as Worksheet;
if (dt.Rows.Count > 0)
int row = 0;
row = dt.Rows.Count; //总行数
int col = dt.Columns.Count; //总列数,可能没用
for (int i = 1; i <= dt.Rows.Count; i++) //
if (dt.Rows[i + 5][0].ToString() == "")
break;
wSheet.Cells[i, 1] = "21";
wSheet.Cells[i, 2] = "19";
wSheet.Cells[i, 3] = year;
wSheet.Cells[i, 4] = month;
wSheet.Cells[i, 5] = djbh;
wSheet.Cells[i, 6] = "";
wSheet.Cells[i, 7] = "吉林瑞宝生产领用";
wSheet.Cells[i, 8] = lyrq;
wSheet.Cells[i, 9] = "R10";//制单人
wSheet.Cells[i, 10] = "";
wSheet.Cells[i, 11] = "";
wSheet.Cells[i, 12] = "5001-97R";
wSheet.Cells[i, 13] = "1";
wSheet.Cells[i, 14] = "RMB";
wSheet.Cells[i, 15] = xmgd;//项目工段
wSheet.Cells[i, 16] = cpdm;
wSheet.Cells[i, 17] = "2008-04-07";
wSheet.Cells[i, 18] = "2008-04-07";
wSheet.Cells[i, 19] = "";
wSheet.Cells[i, 20] = "";
wSheet.Cells[i, 21] = "0";
wSheet.Cells[i, 22] = "0";
wSheet.Cells[i, 23] = "";
wSheet.Cells[i, 24] = ",";
wSheet.Cells[i, 25] = "";
wSheet.Cells[i, 26] = ",";
wSheet.Cells[i, 27] = "";
wSheet.Cells[i, 28] = "";
wSheet.Cells[i, 29] = i.ToString();//行号
wSheet.Cells[i, 30] = dt.Rows[i+ 5][0].ToString();
wSheet.Cells[i, 31] = "";
wSheet.Cells[i, 32] = "'"+dt.Rows[i + 5][11].ToString(); //货位
wSheet.Cells[i, 33] = dt.Rows[i + 5][2].ToString();//计量单位
wSheet.Cells[i, 34] = dt.Rows[i + 5][8].ToString();//实发数量
wSheet.Cells[i, 35] = dt.Rows[i + 5][9].ToString();//计划单价
wSheet.Cells[i, 36] = "";
wSheet.Cells[i, 37] = "100";
wSheet.Cells[i, 38] = double.Parse(dt.Rows[i + 5][8].ToString()) * double.Parse(dt.Rows[i + 5][9].ToString());
wSheet.Cells[i, 39] = double.Parse(dt.Rows[i + 5][8].ToString()) * double.Parse(dt.Rows[i + 5][9].ToString());
wSheet.Cells[i, 40] = "";
wSheet.Cells[i, 41] = "0";
wSheet.Cells[i, 42] = "0";
wSheet.Cells[i, 43] = "";
wSheet.Cells[i, 44] = "";
wSheet.Cells[i, 45] = "";
wSheet.Cells[i, 46] = "0";
wSheet.Cells[i, 47] = "";
wSheet.Cells[i, 48] = "";
wSheet.Cells[i, 49] = "";
wSheet.Cells[i, 50] = "";
wSheet.Cells[i, 51] = "";
wSheet.Cells[i, 52] = "";
wSheet.Cells[i, 53] = "";
wSheet.Cells[i, 54] = "";
wSheet.Cells[i, 55] = "0";
wSheet.Cells[i, 56] = double.Parse(dt.Rows[i + 5][8].ToString()) * double.Parse(dt.Rows[i + 5][9].ToString());
wSheet.Cells[i, 57] = "0";
wSheet.Cells[i, 58] = "0";
wSheet.Cells[i, 59] = "0";
wSheet.Cells[i, 60] = double.Parse(dt.Rows[i + 5][8].ToString()) * double.Parse(dt.Rows[i + 5][9].ToString());
wSheet.Cells[i, 61] = "";
wSheet.Cells[i, 62] = "";
wSheet.Cells[i, 63] = "1003";
wSheet.Cells[i, 64] = "R027";
wSheet.Cells[i, 65] = "";
wSheet.Cells[i, 66] = "";
wSheet.Cells[i, 67] = "";
wSheet.Cells[i, 68] = "0";
wSheet.Cells[i, 69] = "0";
wSheet.Cells[i, 70] = "";
wSheet.Cells[i, 71] = "";
wSheet.Cells[i, 72] = "0";
wSheet.Cells[i, 73] = "0";
wSheet.Cells[i, 74] = "";
wSheet.Cells[i, 75] = "";
wSheet.Cells[i, 76] = "0001-05";
wSheet.Cells[i, 77] = "2";
wSheet.Cells[i, 78] = "";
wSheet.Cells[i, 79] = "1";
wSheet.Cells[i, 80] = "";
wSheet.Cells[i, 81] = "";
wSheet.Cells[i, 82] = "0";
wSheet.Cells[i, 83] = "";
wSheet.Cells[i, 84] = "";
wSheet.Cells[i, 85] = "";
wSheet.Cells[i, 86] = "";
wSheet.Cells[i, 87] = "";
wSheet.Cells[i, 88] = "";
wSheet.Cells[i, 89] = "";
wSheet.Cells[i, 90] = "";
wSheet.Cells[i, 91] = "";
wSheet.Cells[i, 92] = "";
wSheet.Cells[i, 93] = "";
wSheet.Cells[i, 94] = "";
wSheet.Cells[i, 95] = "";
wSheet.Cells[i, 96] = "";
wSheet.Cells[i, 97] = "";
wSheet.Cells[i, 98] = "";
wSheet.Cells[i, 99] = "";
wSheet.Cells[i, 100] = "";
wSheet.Cells[i, 101] = "";
wSheet.Cells[i, 102] = "";
wSheet.Cells[i, 103] = "";
wSheet.Cells[i, 104] = "";
wSheet.Cells[i, 105] = "";
wSheet.Cells[i, 106] = "";
wSheet.Cells[i, 107] = "";
wSheet.Cells[i, 108] = "";
wSheet.Cells[i, 109] = "";
wSheet.Cells[i, 110] = "";
wSheet.Cells[i, 111] = "";
wSheet.Cells[i, 112] = "";
wSheet.Cells[i, 113] = "";
wSheet.Cells[i, 114] = "";
wSheet.Cells[i, 115] = "";
wSheet.Cells[i, 116] = "";
wSheet.Cells[i, 117] = "";
wSheet.SaveAs(savefilepath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
wBook.Close(false, Missing.Value, Missing.Value);//关闭
wSheet = null;
wBook = null;
app.Quit();
app = null;
//int size = excelTable.Columns.Count;
//for (int i = 0; i < size; i++)
//
// wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName;
//
//设置禁止弹出保存和覆盖的询问提示框
//app.DisplayAlerts = false;
//app.AlertBeforeOverwriting = false;
////保存工作簿
//wBook.Save();
////保存excel文件
//app.Save("C:\a.xls");
//app.SaveWorkspace("C:\a.xls");
MessageBox.Show("转换成功!");
return true;
catch (Exception err)
MessageBox.Show("导出Excel出错!错误原因:" + err.Message, "提示信息",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
finally
//public static bool SaveDataTableToExcel(System.Data.DataTable excelTable, string filePath)
//
// Microsoft.Office.Interop.Excel.Application app =
// new Microsoft.Office.Interop.Excel.ApplicationClass();
//
public static System.Data.DataTable ExcelToDT(string filename, ref System.Data.DataTable dt)
//先将EXCEL导入到数据库,一:先把EXCEL导入dateView,二:然后将dateView里的数据导入到数据库里面
//EXCEL 的连接串
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + filename + ";" +
"Extended Properties='Excel 8.0;IMEX=1'";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
System.Data.DataTable resultdt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[4] );
objConn.Close();
try
foreach (DataRow dr in resultdt.Rows)
//这里的删掉一些找不到的sheet,类似sheet1_
if (dr["Table_Name"].ToString().IndexOf('_') < 0)
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + dr["Table_Name"].ToString() + "]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
//建立EXCEL的连接
objConn.Open();
objAdapter1.Fill(objDataset1);
objConn.Close();
// DataGrid1.DataSource = objDataset1.Tables[0].DefaultView; //测试代码,用来测试是否能读出EXCEL上面的数据
// DataGrid1.DataBind();
//这里的tmpdt就是读到的当前sheet的dt了
System.Data.DataTable tmpdt = objDataset1.Tables[0];
dt = tmpdt;
string fileName = "";
fileName = filename.Substring(filename.LastIndexOf('\\') + 1);
break;
return dt;
catch (Exception e)
if (objConn.State == ConnectionState.Open)
objConn.Close();
string s = e.Message;
Console.Write("转换操作出错");
return null;
另外,虚机团上产品团购,超级便宜 参考技术B 后边有点看不太清楚,如果改起来不是那么麻烦,还是建议楼主手动改一下,改成您说的那种标准的形式,就可以了。
asp net如何读取客户端的ini文件
参考技术A 读不了,没权限的。只能上传到服务器端再读。参考资料:http://www.newasp.net/tech/net/10146.html
以上是关于ASP.net读取Excel文件的问题的主要内容,如果未能解决你的问题,请参考以下文章
在 asp.net core 1.0 上读取一个 excel 文件