(大会でそのコストの正確さとトータルコストも得点になる)
締め切りが今日の15時なんですよね。
で、パーツ数が約600、それぞれの図面と
あとコストを算出するエクセルファイルがあるんですよ。
で、それについてはどうでもいいのですが、それぞれのエクセルファイルに記載されたコストを
最終的に一つの表にまとめ上げて目次みたいなものを作らなきゃならんのですね。
・・・しかしパーツの数が600もあって、しかもまだエクセルファイルが90%くらいしか完成してないわけですよ。
一つのパーツから入力すべき数字は6つ、しかもパーツ単位で一つのファイルではなく、
人によってはシートで管理していたりとカオス。
しかもパーツ名が目次と対応していないもの多数!
去年の経験上これを人力で行うには最低3時間必要
人力で表を埋めるのはもはや現実的ではないと判断し、
コストのエクセルを読み込んで、自動で目次のエクセルを出力するプログラムをさっき書きました。
現在バックグラウンドで実行中の
構想1分コーディング15分デバッグ10分のコードがこちら
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
namespace ExcelTracerForCost
{
class Program
{
static void Main()
{
StreamWriter w = new StreamWriter(@"C:\cost\log.txt");
Dictionary names = new Dictionary();
Excel.Application objExcel = new Excel.Application();
Excel.Workbook objWorkbook = null;
Excel.Worksheet objWorkSheet = null;
try
{
objWorkbook = objExcel.Workbooks.Open(Filename: @"C:\019_FSAEJ_CR.xls");
objWorkSheet = (Excel.Worksheet)objWorkbook.Sheets[2];
int r = objWorkSheet.UsedRange.Rows.Count;
for (int i = 7; i < r; ++i)
{
Excel.Range range = (Excel.Range)objWorkSheet.Cells[i, 6];
if (range.Value2 != null)
{
string value = range.Value2.ToString();
value = value.Replace(" ", "");
names[value] = i;
}
}
}
catch (Exception e)
{
int a = 0;
}
foreach (string strFilePath in System.IO.Directory.GetFiles( @"C:\cost", "*.xls",System.IO.SearchOption.AllDirectories))
{
Excel.Workbook tempBook = null;
Excel.Worksheet tempSheet = null;
tempBook = objExcel.Workbooks.Open(Filename: strFilePath);
for (int i = 1; i <= tempBook.Sheets.Count; ++i)
{
tempSheet = (Excel.Worksheet)tempBook.Sheets[i];
bool isAsm = false;
bool isPart = false;
Excel.Range range = (Excel.Range)tempSheet.Cells[3, 1];
string value = "";
if( range.Value2 != null)
value = range.Value2.ToString();
if (value.Equals("Assembly"))
{
range = (Excel.Range)tempSheet.Cells[4, 1];
value = range.Value2.ToString();
if (value.Equals("Part"))
{
isPart = true;
}
else
{
isAsm = true;
}
}
else
{
continue;
}
string name = tempSheet.Name;
if (isPart)
{
range = (Excel.Range)tempSheet.Cells[4, 2];
string partName = "";
try
{
partName = range.Value2.ToString();
//Console.Out.WriteLine("パーツ名がありません");
// Console.Out.WriteLine(tempSheet.Name);
}
catch (Exception e)
{
continue;
}
string MaterialCost = "0";
string ProcessCost = "0";
string FastenerCost = "0";
string ToolingCost = "0";
int r = tempSheet.UsedRange.Rows.Count;
for (int j = 1; j < r; ++j)
{
range = (Excel.Range)tempSheet.Cells[j, 8];
try
{
value = range.Value2.ToString();
if (value.Equals("Sub Total") && ProcessCost == "0")
{
range = (Excel.Range)tempSheet.Cells[j, 9];
ProcessCost = range.Value2.ToString();
continue;
}
}
catch (Exception e)
{
}
range = (Excel.Range)tempSheet.Cells[j, 13];
try
{
value = range.Value2.ToString();
if (value.Equals("Sub Total"))
{
range = (Excel.Range)tempSheet.Cells[j, 14];
MaterialCost = range.Value2.ToString();
continue;
}
}
catch (Exception e)
{
}
range = (Excel.Range)tempSheet.Cells[j, 9];
try
{
value = range.Value2.ToString();
if (value.Equals("Sub Total"))
{
range = (Excel.Range)tempSheet.Cells[j, 10];
FastenerCost = range.Value2.ToString();
continue;
}
}
catch (Exception e)
{
}
range = (Excel.Range)tempSheet.Cells[j, 8];
try
{
value = range.Value2.ToString();
if (value.Equals("Sub Total") && !ProcessCost.Equals("0"))
{
range = (Excel.Range)tempSheet.Cells[j, 9];
ToolingCost = range.Value2.ToString();
continue;
}
}
catch (Exception e)
{
}
}
int objRow = 0;
partName = partName.Replace(" ", "");
if (names.TryGetValue(partName, out objRow))
{
objWorkSheet.Cells[objRow, 10] = MaterialCost;
objWorkSheet.Cells[objRow, 11] = ProcessCost;
objWorkSheet.Cells[objRow, 12] = FastenerCost;
objWorkSheet.Cells[objRow, 13] = ToolingCost;
}
else
{
Console.Out.WriteLine("パーツ名がBOMと一致しません");
Console.Out.WriteLine(partName);
w.WriteLine("パーツ名がBOMと一致しません");
w.WriteLine(partName);
}
}
else if (isAsm)
{
range = (Excel.Range)tempSheet.Cells[3, 2];
string asmName = "";
try
{
asmName = range.Value2.ToString();
//Console.Out.WriteLine("アセンブリ名がありません");
//Console.Out.WriteLine(tempSheet.Name);
}
catch (Exception e)
{
continue;
}
string MaterialCost = "0";
string ProcessCost = "0";
string FastenerCost = "0";
string ToolingCost = "0";
int asms = 0;
range = (Excel.Range)tempSheet.Cells[2, 14];
if (range.Value2 != null)
{
value = range.Value2.ToString();
int.TryParse(value, out asms);
}
else
{
Console.Out.WriteLine("アセンブリの個数がありません");
Console.Out.WriteLine(tempSheet.Name);
w.WriteLine("アセンブリの個数がありません");
w.WriteLine(tempSheet.Name);
}
int r = tempSheet.UsedRange.Rows.Count;
for (int j = 1; j < r; ++j)
{
range = (Excel.Range)tempSheet.Cells[j, 8];
try
{
value = range.Value2.ToString();
if (value.Equals("Sub Total") && ProcessCost == "0")
{
range = (Excel.Range)tempSheet.Cells[j, 9];
ProcessCost = range.Value2.ToString();
continue;
}
}
catch (Exception e)
{
}
range = (Excel.Range)tempSheet.Cells[j, 13];
try
{
value = range.Value2.ToString();
if (value.Equals("Sub Total"))
{
range = (Excel.Range)tempSheet.Cells[j, 14];
MaterialCost = range.Value2.ToString();
continue;
}
}
catch (Exception e)
{
}
range = (Excel.Range)tempSheet.Cells[j, 9];
try
{
value = range.Value2.ToString();
if (value.Equals("Sub Total"))
{
range = (Excel.Range)tempSheet.Cells[j, 10];
FastenerCost = range.Value2.ToString();
continue;
}
}
catch (Exception e)
{
}
range = (Excel.Range)tempSheet.Cells[j, 8];
try
{
value = range.Value2.ToString();
if (value.Equals("Sub Total") && !ProcessCost.Equals("0"))
{
range = (Excel.Range)tempSheet.Cells[j, 9];
ToolingCost = range.Value2.ToString();
continue;
}
}
catch (Exception e)
{
}
range = (Excel.Range)tempSheet.Cells[j, 2];
try
{
int row = 0;
value = range.Value2.ToString();
value = value.Replace(" ", "");
if ( names.TryGetValue(value, out row) )
{
range = (Excel.Range)tempSheet.Cells[j, 4];
value = range.Value2.ToString();
int parts = 0;
int.TryParse(value,out parts);
objWorkSheet.Cells[row, 9] = asms * parts;
continue;
}
}
catch (Exception e)
{
int a = 0;
}
}
int objRow = 0;
asmName = asmName.Replace(" ", "");
if (names.TryGetValue(asmName, out objRow))
{
objWorkSheet.Cells[objRow, 9] = asms;
objWorkSheet.Cells[objRow, 10] = MaterialCost;
objWorkSheet.Cells[objRow, 11] = ProcessCost;
objWorkSheet.Cells[objRow, 12] = FastenerCost;
objWorkSheet.Cells[objRow, 13] = ToolingCost;
}
else
{
Console.Out.WriteLine("アセンブリ名がBOMと一致しません");
Console.Out.WriteLine(asmName);
w.WriteLine("アセンブリ名がBOMと一致しません");
w.WriteLine(asmName);
}
}
}
Console.Out.WriteLine(strFilePath);
w.WriteLine(strFilePath);
tempBook.Saved = true;
tempBook.Close(false);
}
objWorkbook.SaveAs(@"C:\output.xls");
objWorkbook.Saved = true;
objWorkbook.Close(false);
Marshal.ReleaseComObject(objWorkSheet);
Marshal.ReleaseComObject(objWorkbook);
objExcel.Quit();
Marshal.ReleaseComObject(objExcel);
}
}
}
もはやカオスwwコピペ多数。関数?カプセル化?なにそれおいしいの?
そして例外をことごとく握りつぶしたり、途中で止まったらエクセルが終了しなかったりとバグ多数のコードですw
いいんだよ!もう時間がないんだw
時間制限つきでまともなコードが書けるようになりたい・・・orz
でもいま結果見たらちゃんと出力できてたので良しとします。(BOMとは目次のことです。)