我正在为我们的工程师制作一份表格,旨在“自动化”我们的物料清单生成。我们有一个名为AutoAttibute(由Cadig使用)的程序,它与AutoCAD一起使用,获取每个选定“块”的值并将其输出到excel文件。没有选项将其输出为任何其他格式(SQL,Access等)。
AutoAttribute生成的表的基本布局如下:
Material | Total Length Weight
--------------------------------
name1 | 34
name1 | 34
name2 | 34
name2 | 34
name3 | 34
name3 | 34
name4 | 33
name4 | 33
name5 | 1254
name6 | 324
还有其他列已生成,但这些是我需要关注的。
创建此文件后,我们一直使用MS Access获取第二列中值的总和。
以下是查询:
我没有我们使用的确切查询,但基本上是:
Select Distinct [Material],
[Total Length Weight]
from `table`
where [Material] <> ''
哪位给了我:
Material | Total Length Weight
--------------------------------
name1 | 34
name2 | 34
name3 | 34
name4 | 33
name5 | 1254
name6 | 324
然后,在一个单独的查询中:
Select Sum [Total Length Weight]
From Query 1
哪位给了我:
Sum([Total Length Weight])
----------------------------
1713
然后我们可以使用该值来做其他事情,比如在另一个显示项目总重量的公式中用作变量。
我现在正在使用C#创建一个全新的表单,我在重新创建查询时遇到了问题。
我正在使用连接字符串:
string file = "D:\\Cut-Lengths\\13-1311 CUT-LENGTHS.xlsx";
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";Extended Properties=Excel 12.0";
然后,我有以下问题:
string query = "Select Sum([CUT-LENGTH-WEIGHT]) AS [SumOfCUT-LENGTH-WEIGHT], Sum([TOTAL-SHEETING-WEIGHT]) AS [SumOfTOTAL-SHEETING-WEIGHT], Sum([TOTAL-ITEM-WEIGHT]) AS [SumOfTOTAL-ITEM-WEIGHT] From [Sheet1$]";
string query2 = "Select COUNT([Total-Length-Weight]) AS `Total Length Weight` From [Sheet1$]";
string query3 = "Select DISTINCT [Total-Length-Weight] AS `Total Length Weight`, [Material] AS `Material`, [Order] From [Sheet1$] WHERE [Material] <> '' ORDER BY [Order]";
string table = "Select * From [Sheet1$]";
我使用这些查询来填充4个dataGridViews,以及表单上的几个TextBox。
我知道, query2
是非常错误的。这是我试图从Access重新创建查询的地方,但无论我尝试什么,我都无法让它工作。
我尝试的同一查询的其他方式:
string query2 = "Select Sum(DISTINCT [Total-Length-Weight]) AS `Total Length Weight` From [Sheet1$]";
string query2 = "Select DISTINCT [material], Sum([Total-Length-Weight]) AS `Total Length Weight` From [Sheet1$]";
此刻我还记不起其他人。我知道其中一个涉及从未加载过表单的子查询。
我真的需要帮助,而我只是不知道我做错了什么。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Xml.Serialization;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace excelConnectiontests
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SetFirstRowSelected()
{
if (gvExcelData.Rows.Count > 0)
{
var row = gvExcelData.Rows[0];
var row2 = gvExcelData2.Rows[0];
var row3 = gvExcelData3.Rows[0];
var row4 = gvExcelData4.Rows[0];
txtCutLengthWeight.Text = row.Cells[0].Value.ToString();
txtTotalSheetingWeight.Text = row.Cells[1].Value.ToString();
txtTotalItemWeight.Text = row.Cells[2].Value.ToString();
//and so on
int value1 = Convert.ToInt32(row.Cells[0].Value);
int value2 = Convert.ToInt32(row.Cells[1].Value);
int value3 = Convert.ToInt32(row.Cells[2].Value);
int value4 = 0;
if (txtCutSheetingWeight.Text != "")
{
value4 = Convert.ToInt32(txtCutSheetingWeight.Text);
}
else
{
value4 = 0;
}
txtNetWeight.Text = (value1 + value2 + value3 + value4).ToString();
gvExcelData.CurrentCell = row.Cells[0]; //set focus to first cell in first row
}
}
private void Form1_Load(object sender, EventArgs e)
{
string file = "D:\\Cut-Lengths\\13-1311 CUT-LENGTHS.xlsx";
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";Extended Properties=Excel 12.0";
string CutTubes = "";
string CutSheeting = "";
string UncutTubes = "";
string UncutSheeting = "";
string NonSteel = "";
string p31 = "";
string query = "Select Sum([CUT-LENGTH-WEIGHT]) AS [SumOfCUT-LENGTH-WEIGHT], Sum([TOTAL-SHEETING-WEIGHT]) AS [SumOfTOTAL-SHEETING-WEIGHT], Sum([TOTAL-ITEM-WEIGHT]) AS [SumOfTOTAL-ITEM-WEIGHT] From [Sheet1$]";
string query2 = "Select COUNT([Total-Length-Weight]) AS `Total Length Weight` From [Sheet1$]";
string query3 = "Select DISTINCT [Total-Length-Weight] AS `Total Length Weight`, [Material] AS `Material`, [Order] From [Sheet1$] WHERE [Material] <> '' ORDER BY [Order]";
string table = "Select * From [Sheet1$]";
OleDbConnection connObj = new OleDbConnection(connString);
connObj.Open();
// Uncut Steel
OleDbCommand cmdObj4 = new OleDbCommand(query3, connObj);
OleDbDataAdapter daObj4 = new OleDbDataAdapter(cmdObj4);
DataTable dtObj4 = new DataTable();
daObj4.Fill(dtObj4);
gvExcelData4.DataSource = dtObj4;
// Totals
OleDbCommand cmdObj3 = new OleDbCommand(table, connObj);
OleDbDataAdapter daObj3 = new OleDbDataAdapter(cmdObj3);
DataTable dtObj3 = new DataTable();
daObj3.Fill(dtObj3);
gvExcelData3.DataSource = dtObj3;
// Length Weight
OleDbCommand cmdObj = new OleDbCommand(query, connObj);
OleDbDataAdapter daObj = new OleDbDataAdapter(cmdObj);
DataTable dtObj = new DataTable();
daObj.Fill(dtObj);
gvExcelData.DataSource = dtObj;
// Total Length Weight
OleDbCommand cmdObj2 = new OleDbCommand(query2, connObj);
OleDbDataAdapter daObj2 = new OleDbDataAdapter(cmdObj2);
DataTable dtObj2 = new DataTable();
daObj2.Fill(dtObj2);
gvExcelData2.DataSource = dtObj2;
connObj.Close();
SetFirstRowSelected();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
SetFirstRowSelected();
}
}
}
我想出了我需要的东西:
string query2 = "SELECT SUM([Total-Length-Weight]) FROM (Select DISTINCT [Material], [Total-Length-Weight] from [Sheet1$])";
感谢大家的帮助。
答案 0 :(得分:0)
之前我还没有使用Excel查询,但语法看起来像T-SQL。如果是这种情况,我认为您正在寻找的是group by
:
string query2 = "Select [material], Sum([Total-Length-Weight]) AS `Total Length Weight` From [Sheet1$] group by [material]";
答案 1 :(得分:0)
我认为你需要group by
,所以:
Select [Material], SUM([Total Length Weight])
from `table`
group by [Material]
having [Material] <> ''
修改强>
如果我们关心每种材料只有一个重量,并且我们可以假设所有条目总是相同的,那么我们可以欺骗并使用MIN
或MAX
,例如:
SELECT SUM(Weight) FROM
(Select [Material], MAX([Total Length Weight]) 'Weight'
from `table`
group by [Material]
having [Material] <> '' )
答案 2 :(得分:0)
我认为您可以通过GROUP BY修复查询2。
因此对于查询2:
&#34;选择[材料],总和([总长度 - 重量])AS Total Length Weight
从[Sheet1 $]组按[material]&#34;;
答案 3 :(得分:0)
每个人都忽略的是,您只想对不同的值求和,以下代码应该这样做:
Select [material], Sum(DISTINCT [Total-Length-Weight]) AS `Total Length Weight`
From [Sheet1$]
where [material] <> ''
group by [material]
答案 4 :(得分:0)
我明白了! :)
这是我需要的查询:
string query2 = "SELECT SUM([Total-Length-Weight]) FROM (Select DISTINCT [Material], [Total-Length-Weight] from [Sheet1$])";