python 文件上传钉盘_钉钉接口 给用户发钉盘文件消息 钉盘文件上传

public classExportExcel

{public static voidExportToText(System.Data.DataTable dt, String filename)

{

ExportToText(dt, filename,false);

}///

///快速导出文本///

///

///

public static void ExportToText(System.Data.DataTable dt, String filename, boolisFirst)

{if (dt == null) return;string strPath =filename;try{//先打印标头

StringBuilder strColu= newStringBuilder();

StringBuilder strValue= newStringBuilder();string strColText = string.Empty;string strColHeadText = string.Empty;

StreamWriter sw= null;if(File.Exists(filename))

{

sw= new StreamWriter(new FileStream(strPath, FileMode.Append), Encoding.GetEncoding("GB2312"));

}else{

sw= new StreamWriter(new FileStream(strPath, FileMode.Create), Encoding.GetEncoding("GB2312"));

}using(sw)

{

sw.AutoFlush= true;if(isFirst)

{//先打印表头

for (int i = 0; i <= dt.Columns.Count - 1; i++)

{

strColHeadText=dt.Columns[i].ColumnName;if (string.IsNullOrEmpty(strColHeadText))

{

strColHeadText=TWays.Utils.ToString(dt.Columns[i].ColumnName);

}

strColu.Append(strColHeadText);

strColu.Append("\t");

}

strColu.Remove(strColu.Length- 1, 1);//移出掉最后一个,字符

}

sw.WriteLine(strColu);for (int i = 0; i < dt.Rows.Count; i++)

{

DataRow dr=dt.Rows[i];if (dr != null)

{

strValue.Remove(0, strValue.Length);//移出

for (int j = 0; j <= dt.Columns.Count - 1; j++)

{

strColText= string.Empty;if (dr[j] != null)

{

strColText=dr[j].ToString();

}

strValue.Append(strColText);

strValue.Append("\t");

}

strValue.Remove(strValue.Length- 1, 1);//移出掉最后一个,字符

sw.WriteLine(strValue);

}

}

sw.Flush();

sw.Close();

sw.Dispose();

}

}catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}///

///快速导出文本///

///

///

public static voidExportToTextForBudget(System.Data.DataTable dt, System.Data.DataTable dtDate, String filename)

{if (dt == null) return;string strPath =filename;try{//先打印标头

bool isFirst = true;

StringBuilder strColu= newStringBuilder();

StringBuilder strValue= newStringBuilder();string strColText = string.Empty;string strColHeadText = string.Empty;

StreamWriter sw= null;if(File.Exists(filename))

{

sw= new StreamWriter(new FileStream(strPath, FileMode.Append), Encoding.GetEncoding("GB2312"));

}else{

sw= new StreamWriter(new FileStream(strPath, FileMode.Create), Encoding.GetEncoding("GB2312"));

}using(sw)

{

sw.AutoFlush= true;if(isFirst)

{string strAmtColName = string.Empty;//先打印表头

for (int i = 0; i <= dt.Columns.Count - 1; i++)

{

strColHeadText=dt.Columns[i].ColumnName;if (strColHeadText.ToUpper().Contains("_AMT_"))

{

DataRow[] dr= dtDate.Select("COLUMN_NAME = '" + strColHeadText + "'");if (dr.Length > 0)

{

strColHeadText= TWays.Utils.ToString(dr[0]["COLUMN_VALUE"]);

}

}else if (strColHeadText.ToUpper().Contains("A"))

{

strColHeadText= strColHeadText.Replace("A", "");

}

strColu.Append(strColHeadText);

strColu.Append("\t");

}

strColu.Remove(strColu.Length- 1, 1);//移出掉最后一个,字符

}

sw.WriteLine(strColu);for (int i = 0; i < dt.Rows.Count; i++)

{

DataRow dr=dt.Rows[i];if (dr != null)

{

strValue.Remove(0, strValue.Length);//移出

for (int j = 0; j <= dt.Columns.Count - 1; j++)

{

strColText= string.Empty;if (dr[j] != null)

{

strColText=dr[j].ToString();

}

strValue.Append(strColText);

strValue.Append("\t");

}

strValue.Remove(strValue.Length- 1, 1);//移出掉最后一个,字符

sw.WriteLine(strValue);

}

}

sw.Flush();

sw.Close();

sw.Dispose();

}

}catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}///

///通过StreamWriter写///

///

///

///

///

///

public static bool WriteToText(DataSet dataSet, DataSet dsWeek, string strOperDate, string fileName, boolisFirst)

{string strPath =fileName;

System.Data.DataTable dataTable= dataSet.Tables[0];int rowNumber = dataTable.Rows.Count;//不包括字段名

if (rowNumber == 0)

{return false;

}

StringBuilder sb= newStringBuilder();

StringBuilder sbValue= newStringBuilder();

StreamWriter sw= null;if(File.Exists(fileName))

{

sw= new StreamWriter(new FileStream(strPath, FileMode.Append), Encoding.GetEncoding("GB2312"));

}else{

sw= new StreamWriter(new FileStream(strPath, FileMode.Create), Encoding.GetEncoding("GB2312"));

}using(sw)

{string strColName = string.Empty;string[] strWeekNum;intiWeekNum;string strWeekColName = string.Empty;

sw.AutoFlush= true;try{if(isFirst)

{int iColIndex = 0;//生成字段名称

foreach (DataColumn col indataTable.Columns)

{

strColName=col.ColumnName;if (strColName.Contains("WEEK_"))

{

strWeekNum= strColName.Split(new string[] { "_"}, StringSplitOptions.RemoveEmptyEntries);

iWeekNum= Convert.ToInt32(strWeekNum[2]);

strWeekColName= Convert.ToDateTime(TWays.Utils.ToString(dsWeek.Tables[0].Select("WEEK_NUM =" + iWeekNum + "AND YEAR =" + Convert.ToInt32(strWeekNum[1]))[0]["BEGIN_DATE"])).ToString("MM/dd");

strColName=strWeekColName;

}else if (strColName.Contains("DAY_"))

{

strWeekNum= strColName.Split(new string[] { "_"}, StringSplitOptions.RemoveEmptyEntries);

iWeekNum= Convert.ToInt32(strWeekNum[1]);

strWeekColName= Convert.ToDateTime(strOperDate).AddDays(-iWeekNum).ToString("MM/dd");

strColName=strWeekColName;

}

sb.Append(strColName);

sb.Append("\t");

iColIndex++;

}

sb.Remove(sb.Length- 1, 1);//移出掉最后一个,字符

sw.WriteLine(sb);

}string strValue = string.Empty;//生成数据

for (int i = 0; i < dataTable.Rows.Count; i++)

{

sbValue.Remove(0, sbValue.Length);//移出

for (int j = 0; j < dataTable.Columns.Count; j++)

{

strValue=TWays.Utils.ToString(dataTable.Rows[i][j]);

strValue= strValue.Replace("\r", "");

strValue= strValue.Replace("\n", "");

strValue= strValue.Replace("\t", "");

strValue= strValue.Replace("\"", "");

sbValue.Append(strValue);

sbValue.Append("\t");

}

sbValue.Remove(sbValue.Length- 1, 1);//移出掉最后一个,字符

sw.WriteLine(sbValue);

}

sw.Flush();

sw.Close();

sw.Dispose();

}catch(Exception ex)

{throwex;

}finally{

sw= null;

sb= null;

dataTable= null;

dataSet= null;

dsWeek= null;

GC.Collect();

}

}return true;

}///

///添加到压缩文件///

///

///

///

public static string CreateRar(string strzipPath, stringstrtxtPath)

{string strResult = string.Empty;try{if(File.Exists(strzipPath))

{

File.Delete(strzipPath);

}

System.Diagnostics.Process Process1= newSystem.Diagnostics.Process();

Process1.StartInfo.FileName= "Winrar.exe";

Process1.StartInfo.CreateNoWindow= true;//Process1.StartInfo.Arguments = " a -r " + strzipPath + " " + strtxtPath;

Process1.StartInfo.Arguments = "a -ep" + strzipPath + " " +strtxtPath;

Process1.Start();

}catch(Exception ex)

{

strResult=ex.Message;

}returnstrResult;

}///

///导出到Excel///

///

///

///

public static bool DataSetToExcel(DataSet dataSet, stringfileName)

{if(File.Exists(fileName))

{

File.SetAttributes(fileName, FileAttributes.Normal);

File.Delete(fileName);

}

System.Data.DataTable dataTable= dataSet.Tables[0];int rowNumber = dataTable.Rows.Count;//不包括字段名

int columnNumber =dataTable.Columns.Count;int colIndex = 0;//if (rowNumber == 0)//{//return false;//}//建立Excel对象

Microsoft.Office.Interop.Excel.Application excel = newMicrosoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbook workbook=excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

Microsoft.Office.Interop.Excel.Worksheet worksheet= (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

excel.Visible= false;

Microsoft.Office.Interop.Excel.Range range;//生成字段名称

foreach (DataColumn col indataTable.Columns)

{

colIndex++;

excel.Cells[1, colIndex] =col.ColumnName;

}object[,] objData = new object[rowNumber, columnNumber];for (int r = 0; r < rowNumber; r++)

{for (int c = 0; c < columnNumber; c++)

{

objData[r, c]=dataTable.Rows[r][c];

}

}//写入Excel

range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]);

range.NumberFormat= "@";//设置单元格为文本格式

range.Value2 =objData;

workbook.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);try{

workbook.Saved= true;

excel.UserControl= false;

}catch(Exception exception)

{

MessageBox.Show(exception.Message);

}finally{

workbook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Missing.Value, Missing.Value);

excel.Quit();

workbook= null;

worksheet= null;

excel= null;

range= null;

}return true;

}///

///导出到Excel///

///

///

///

public static bool DataSetToExcel(DataSet dataSet, DataSet dsWeek, string strOperDate, stringfileName)

{if(File.Exists(fileName))

{

File.Delete(fileName);

}

System.Data.DataTable dataTable= dataSet.Tables[0];int rowNumber = dataTable.Rows.Count;//不包括字段名

int columnNumber =dataTable.Columns.Count;int colIndex = 0;if (rowNumber == 0)

{return false;

}//建立Excel对象

Microsoft.Office.Interop.Excel.Application excel = newMicrosoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbook workbook=excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

Microsoft.Office.Interop.Excel.Worksheet worksheet= (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];

excel.Visible= false;

Microsoft.Office.Interop.Excel.Range range;string strColName = string.Empty;string[] strWeekNum;intiWeekNum;string strWeekColName = string.Empty;//生成字段名称

foreach (DataColumn col indataTable.Columns)

{

strColName=col.ColumnName;if (strColName.Contains("WEEK_"))

{

strWeekNum= strColName.Split(new string[] { "_"}, StringSplitOptions.RemoveEmptyEntries);

iWeekNum= Convert.ToInt32(strWeekNum[2]);

strWeekColName= Convert.ToDateTime(TWays.Utils.ToString(dsWeek.Tables[0].Select("WEEK_NUM =" + iWeekNum + "AND YEAR =" + Convert.ToInt32(strWeekNum[1]))[0]["BEGIN_DATE"])).ToString("MM/dd");

strColName=strWeekColName;

}else if (strColName.Contains("DAY_"))

{

strWeekNum= strColName.Split(new string[] { "_"}, StringSplitOptions.RemoveEmptyEntries);

iWeekNum= Convert.ToInt32(strWeekNum[1]);

strWeekColName= Convert.ToDateTime(strOperDate).AddDays(-iWeekNum).ToString("MM/dd");

strColName=strWeekColName;

}

colIndex++;

excel.Cells[1, colIndex] =strColName;

}object[,] objData = new object[rowNumber, columnNumber];for (int r = 0; r < rowNumber; r++)

{for (int c = 0; c < columnNumber; c++)

{

objData[r, c]=dataTable.Rows[r][c];

}

}//写入Excel

range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]);

range.NumberFormat= "@";//设置单元格为文本格式

range.Value2 =objData;//range.Value = objData;

workbook.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);try{

workbook.Saved= true;

excel.UserControl= false;

GC.Collect();

}catch(Exception exception)

{

MessageBox.Show(exception.Message);

}finally{

workbook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Missing.Value, Missing.Value);

excel.Quit();

workbook= null;

worksheet= null;

excel= null;

range= null;

}return true;

}///

///通过StreamWriter写///

///

///

///

///

///

public static bool WriteToExcel(DataSet dataSet, DataSet dsWeek, string strOperDate, stringfileName)

{if(File.Exists(fileName))

{

File.Delete(fileName);

}

System.Data.DataTable dataTable= dataSet.Tables[0];int rowNumber = dataTable.Rows.Count;//不包括字段名

if (rowNumber == 0)

{return false;

}

StreamWriter sw= new StreamWriter(fileName, false, Encoding.GetEncoding("gb2312"));

StringBuilder sb= newStringBuilder();string strColName = string.Empty;string[] strWeekNum;intiWeekNum;string strWeekColName = string.Empty;try{int iColIndex = 0;//生成字段名称

foreach (DataColumn col indataTable.Columns)

{

strColName=col.ColumnName;if (strColName.Contains("WEEK_"))

{

strWeekNum= strColName.Split(new string[] { "_"}, StringSplitOptions.RemoveEmptyEntries);

iWeekNum= Convert.ToInt32(strWeekNum[2]);

strWeekColName= Convert.ToDateTime(TWays.Utils.ToString(dsWeek.Tables[0].Select("WEEK_NUM =" + iWeekNum + "AND YEAR =" + Convert.ToInt32(strWeekNum[1]))[0]["BEGIN_DATE"])).ToString("MM/dd");

strColName=strWeekColName;

}else if (strColName.Contains("DAY_"))

{

strWeekNum= strColName.Split(new string[] { "_"}, StringSplitOptions.RemoveEmptyEntries);

iWeekNum= Convert.ToInt32(strWeekNum[1]);

strWeekColName= Convert.ToDateTime(strOperDate).AddDays(-iWeekNum).ToString("MM/dd");

strColName=strWeekColName;

}

sb.Append(strColName);if (iColIndex < dataTable.Columns.Count - 1)

{

sb.Append("\t");

}

iColIndex++;

}

sb.Append(Environment.NewLine);string strValue = string.Empty;//生成数据

for (int i = 0; i < dataTable.Rows.Count; i++)

{for (int j = 0; j < dataTable.Columns.Count; j++)

{

strValue=TWays.Utils.ToString(dataTable.Rows[i][j]);

strValue= strValue.Replace("\r", "");

strValue= strValue.Replace("\n", "");

strValue= strValue.Replace("\t", "");

strValue= strValue.Replace("\"", "");

sb.Append(strValue);if (j < dataTable.Columns.Count - 1)

sb.Append("\t");

}

sb.Append(Environment.NewLine);

}

sw.Write(sb.ToString());

sw.Flush();

sw.Close();

}catch(Exception ex)

{throwex;

}finally{

sw= null;

sb= null;

dataTable= null;

dataSet= null;

dsWeek= null;

GC.Collect();

}return true;

}///

///通过StreamWriter写///

///

///

///

///

///

public static bool WriteToExcel(DataSet dataSet, stringfileName)

{if(File.Exists(fileName))

{

File.Delete(fileName);

}

System.Data.DataTable dataTable= dataSet.Tables[0];int rowNumber = dataTable.Rows.Count;//不包括字段名

if (rowNumber == 0)

{return false;

}

StreamWriter sw= new StreamWriter(fileName, false, Encoding.GetEncoding("gb2312"));

StringBuilder sb= newStringBuilder();string strColName = string.Empty;try{int iColIndex = 0;//生成字段名称

foreach (DataColumn col indataTable.Columns)

{

strColName=col.ColumnName;

sb.Append(strColName);if (iColIndex < dataTable.Columns.Count - 1)

{

sb.Append("\t");

}

iColIndex++;

}

sb.Append(Environment.NewLine);string strValue = string.Empty;//生成数据

for (int i = 0; i < dataTable.Rows.Count; i++)

{for (int j = 0; j < dataTable.Columns.Count; j++)

{

strValue=TWays.Utils.ToString(dataTable.Rows[i][j]);

strValue= strValue.Replace("\r", "");

strValue= strValue.Replace("\n", "");

strValue= strValue.Replace("\t", "");

strValue= strValue.Replace("\"", "");

sb.Append(strValue);if (j < dataTable.Columns.Count - 1)

sb.Append("\t");

}

sb.Append(Environment.NewLine);

}

sw.Write(sb.ToString());

sw.Flush();

sw.Close();

}catch(Exception ex)

{throwex;

}finally{

sw= null;

sb= null;

dataTable= null;

dataSet= null;

GC.Collect();

}return true;

}public static System.Data.DataTable GetCsvData(string pCsvpath, stringpCsvname)

{try{

DataSet dsCsvData= newDataSet();

OleDbConnection OleCon= newOleDbConnection();

OleDbCommand OleCmd= newOleDbCommand();

OleDbDataAdapter OleDa= newOleDbDataAdapter();

OleCon.ConnectionString= "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pCsvpath + ";Extended Properties='Text;FMT=Delimited(,);HDR=YES;IMEX=1';";

OleCon.Open();

System.Data.DataTable dts1= OleCon.GetSchema("Tables");

System.Data.DataTable dts2= OleCon.GetSchema("Columns");

OleCmd.Connection=OleCon;

OleCmd.CommandText= "select * from [" + pCsvname + "] where 1=1";

OleDa.SelectCommand=OleCmd;

OleDa.Fill(dsCsvData,"Table");

OleCon.Close();return dsCsvData.Tables[0];

}catch(Exception ex)

{return null;

}

}

}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部