概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
小遣い帳を、エクセルで出力せよ。
写真
サンプルコード
using System;
using System.Text;
using System.IO;
using System.Data;
using System.Data.Common;
using System.Transactions;
using System.Data.SQLite;
using ClosedXML.Excel;
namespace App
{
class Program {
static void Main(string[] args) {
var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Book3.xlsx");
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Sheet1");
int j = 1;
ws.Cell(j, 1).Value = "日付";
ws.Cell(j, 2).Value = "内訳";
ws.Cell(j, 3).Value = "貰ったお金";
ws.Cell(j, 4).Value = "使ったお金";
ws.Cell(j, 5).Value = "残金";
SQLiteConnectionStringBuilder ConnectionStr = new SQLiteConnectionStringBuilder();
ConnectionStr.DataSource = "db0.sqlite";
using (SQLiteConnection Connection = new SQLiteConnection(ConnectionStr.ToString()))
{
Connection.Open();
using(SQLiteCommand Command = new SQLiteCommand(Connection))
{
Command.CommandText = "SELECT * From kozu ORDER BY adate ASC";
using(SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(Command))
{
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
int pre = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
j++;
DataRow row = dt.Rows[i];
ws.Cell(j, 1).Value = row["adate"];
ws.Cell(j, 2).Value = row["memo"];
ws.Cell(j, 3).Value = row["inp"];
ws.Cell(j, 4).Value = row["oup"];
int inp = 0;
int oup = 0;
int zan = 0;
String s2 = Convert.ToString(row["inp"]);
if (s2 != "")
{
inp = Convert.ToInt32(s2);
}
String s3 = Convert.ToString(row["oup"]);
if (s3 != "")
{
oup = Convert.ToInt32(s3);
}
pre = inp - oup + pre;
ws.Cell(j, 5).Value = pre;
}
}
}
}
wb.SaveAs(filePath);
Console.WriteLine("ok");
}
}
}
以上。